{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b0672cfc-bfb4-45fc-a4cb-31d7bcb6a82d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cupy as cp\n",
    "import cupy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f9e200c7-1b34-480e-9ef3-980c920c93f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OS                           : Linux-6.6.84-amd64-desktop-hwe-x86_64-with-glibc2.38\n",
      "Python Version               : 3.12.4\n",
      "CuPy Version                 : 13.4.1\n",
      "CuPy Platform                : NVIDIA CUDA\n",
      "NumPy Version                : 2.2.5\n",
      "SciPy Version                : 1.15.3\n",
      "Cython Build Version         : 3.0.12\n",
      "Cython Runtime Version       : None\n",
      "CUDA Root                    : /usr\n",
      "nvcc PATH                    : /usr/bin/nvcc\n",
      "CUDA Build Version           : 12080\n",
      "CUDA Driver Version          : 12080\n",
      "CUDA Runtime Version         : 12080 (linked to CuPy) / 12020 (locally installed)\n",
      "CUDA Extra Include Dirs      : []\n",
      "cuBLAS Version               : (available)\n",
      "cuFFT Version                : 11008\n",
      "cuRAND Version               : 10303\n",
      "cuSOLVER Version             : (11, 5, 0)\n",
      "cuSPARSE Version             : (available)\n",
      "NVRTC Version                : (12, 2)\n",
      "Thrust Version               : 200800\n",
      "CUB Build Version            : 200800\n",
      "Jitify Build Version         : <unknown>\n",
      "cuDNN Build Version          : (not loaded; try `import cupy.cuda.cudnn` first)\n",
      "cuDNN Version                : (not loaded; try `import cupy.cuda.cudnn` first)\n",
      "NCCL Build Version           : (not loaded; try `import cupy.cuda.nccl` first)\n",
      "NCCL Runtime Version         : (not loaded; try `import cupy.cuda.nccl` first)\n",
      "cuTENSOR Version             : None\n",
      "cuSPARSELt Build Version     : None\n",
      "Device 0 Name                : NVIDIA GeForce MX110\n",
      "Device 0 Compute Capability  : 50\n",
      "Device 0 PCI Bus ID          : 0000:01:00.0\n"
     ]
    }
   ],
   "source": [
    "cp.show_config()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d119a1af-ae41-4a0e-9a65-1a059d1e2216",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_gpu = cp.array([1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e5ca898b-c37f-4cb1-847e-5d50bb5f1a0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_cpu = np.array([1, 2, 3])\n",
    "l2_cpu = np.linalg.norm(x_cpu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1d01de40-5309-47f3-8410-b5e42299dadf",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_gpu = cp.array([1, 2, 3])\n",
    "l2_gpu = cp.linalg.norm(x_gpu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9b62a295-4ce6-4e37-b943-1a805bd9f47a",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_on_gpu0 = cp.array([1, 2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a672ad6d-c16f-48aa-92b0-9e4f7ea4da0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "with cp.cuda.Device(0):\n",
    "   x_on_gpu1 = cp.array([1, 2, 3, 4, 5])\n",
    "x_on_gpu0 = cp.array([1, 2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "afea6545-1e68-42a8-b86b-7b79d1fad62c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<CUDA Device 0>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with cp.cuda.Device(0):\n",
    "   x = cp.array([1, 2, 3, 4, 5])\n",
    "x.device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "08f99c42-c67f-4991-b3f7-f5c571ecaf58",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_cpu = np.array([1, 2, 3])\n",
    "x_gpu = cp.asarray(x_cpu)  # move the data to the current device."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "15549ec3-315c-4fa9-9e8f-7b840f87b1db",
   "metadata": {},
   "outputs": [],
   "source": [
    "with cp.cuda.Device(0):\n",
    "    x_gpu_0 = cp.ndarray([1, 2, 3])  # create an array in GPU 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "724036d1-21bc-4c7e-b43b-3efebbbb6be8",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_gpu = cp.array([1, 2, 3])  # create an array in the current device\n",
    "x_cpu = cp.asnumpy(x_gpu)  # move the array to the host."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2046d6a9-e235-4f57-9025-587925f6d5b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_cpu = x_gpu.get()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6dd86212-55a4-4183-a0f9-9cb5d3b0da6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Stable implementation of log(1 + exp(x))\n",
    "def softplus(x):\n",
    "    xp = cp.get_array_module(x)  # 'xp' is a standard usage in the community\n",
    "    print(\"Using:\", xp.__name__)\n",
    "    return xp.maximum(0, x) + xp.log1p(xp.exp(-abs(x)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "29508998-20f4-4db9-9338-f11df67bfd95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_cpu = np.array([1, 2, 3])\n",
    "y_cpu = np.array([4, 5, 6])\n",
    "x_cpu + y_cpu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c39a38b2-ca21-4d1e-b7fc-c09c2c506ec5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cp.asnumpy(x_gpu) + y_cpu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e248e3ea-5af5-4017-a934-f262a3efc266",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cp.asnumpy(x_gpu) + cp.asnumpy(y_cpu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "28db2bfb-df97-482f-a6e0-71bfebfbd4ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_gpu + cp.asarray(y_cpu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0c0b34ad-76bc-44ba-bf4e-77f0ba71dd4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cp.asarray(x_gpu) + cp.asarray(y_cpu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0612b0be-fbca-40bb-9e2e-4f2f4f5430c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# f(x, y) = (x - y)^2\n",
    "squared_diff = cp.ElementwiseKernel(\n",
    "   'float32 x, float32 y',\n",
    "   'float32 z',\n",
    "   'z = (x - y) * (x - y)',\n",
    "   'squared_diff')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "cfc1b6ae-f242-4160-bc5b-4f455897311b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 1. 2. 3. 4.]\n",
      " [5. 6. 7. 8. 9.]] [0. 1. 2. 3. 4.]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  0.,  0.,  0.,  0.],\n",
       "       [25., 25., 25., 25., 25.]], dtype=float32)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = cp.arange(10, dtype=np.float32).reshape(2, 5)\n",
    "y = cp.arange(5, dtype=np.float32)\n",
    "print(x, y)\n",
    "squared_diff(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "672d078c-ce21-4852-b778-b0539410094c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[25., 16.,  9.,  4.,  1.],\n",
       "       [ 0.,  1.,  4.,  9., 16.]], dtype=float32)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squared_diff(x, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a8d29f2b-6f03-4cf0-ba7e-0544c680bb82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  0.,  0.,  0.,  0.],\n",
       "       [25., 25., 25., 25., 25.]], dtype=float32)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = cp.empty((2, 5), dtype=np.float32)\n",
    "squared_diff(x, y, z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2aeb8878-626d-48bc-916f-c5cab57d4bd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "squared_diff_generic = cp.ElementwiseKernel(\n",
    "    'T x, T y',\n",
    "    'T z',\n",
    "    'z = (x - y) * (x - y)',\n",
    "    'squared_diff_generic')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ef866fa1-4cc9-44ea-b125-fcd113772c10",
   "metadata": {},
   "outputs": [],
   "source": [
    "squared_diff_generic = cp.ElementwiseKernel(\n",
    "    'T x, T y',\n",
    "    'T z',\n",
    "    '''\n",
    "        T diff = x - y;\n",
    "        z = diff * diff;\n",
    "    ''',\n",
    "    'squared_diff_generic')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "0fb3c638-e253-4e01-9604-eb93b44389b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "add_reverse = cp.ElementwiseKernel(\n",
    "    'T x, raw T y', 'T z',\n",
    "    'z = x + y[_ind.size() - i - 1]',\n",
    "    'add_reverse')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6b5e59b8-723c-40c0-8712-19cf9aa3e068",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42.2 μs ± 982 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 5.477226 , 15.9687195], dtype=float32)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l2norm_kernel = cp.ReductionKernel(\n",
    "    'T x',  # input params\n",
    "    'T y',  # output params\n",
    "    'x * x',  # map\n",
    "    'a + b',  # reduce\n",
    "    'y = sqrt(a)',  # post-reduction map\n",
    "    '0',  # identity value\n",
    "    'l2norm'  # kernel name\n",
    ")\n",
    "x = cp.arange(10, dtype=np.float32).reshape(2, 5)\n",
    "%timeit l2norm_kernel(x, axis=1)\n",
    "l2norm_kernel(x, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "76eb967c-f66e-4a3a-bc01-f288bc862071",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  2.,  4.,  6.,  8.],\n",
       "       [10., 12., 14., 16., 18.],\n",
       "       [20., 22., 24., 26., 28.],\n",
       "       [30., 32., 34., 36., 38.],\n",
       "       [40., 42., 44., 46., 48.]], dtype=float32)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add_kernel = cp.RawKernel(r'''\n",
    "extern \"C\" __global__\n",
    "void my_add(const float* x1, const float* x2, float* y) {\n",
    "    int tid = blockDim.x * blockIdx.x + threadIdx.x;\n",
    "    y[tid] = x1[tid] + x2[tid];\n",
    "}\n",
    "''', 'my_add')\n",
    "x1 = cp.arange(25, dtype=cp.float32).reshape(5, 5)\n",
    "x2 = cp.arange(25, dtype=cp.float32).reshape(5, 5)\n",
    "y = cp.zeros((5, 5), dtype=cp.float32)\n",
    "add_kernel((5,), (5,), (x1, x2, y))  # grid, block and arguments\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "fe09495a-8572-4161-b728-b252e9fac1e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0. +0.j,  1. +2.j,  2. +4.j,  3. +6.j,  4. +8.j],\n",
       "       [ 5.+10.j,  6.+12.j,  7.+14.j,  8.+16.j,  9.+18.j],\n",
       "       [10.+20.j, 11.+22.j, 12.+24.j, 13.+26.j, 14.+28.j],\n",
       "       [15.+30.j, 16.+32.j, 17.+34.j, 18.+36.j, 19.+38.j],\n",
       "       [20.+40.j, 21.+42.j, 22.+44.j, 23.+46.j, 24.+48.j]],\n",
       "      dtype=complex64)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "complex_kernel = cp.RawKernel(r'''\n",
    "#include <cupy/complex.cuh>\n",
    "extern \"C\" __global__\n",
    "void my_func(const complex<float>* x1, const complex<float>* x2,\n",
    "             complex<float>* y, float a) {\n",
    "    int tid = blockDim.x * blockIdx.x + threadIdx.x;\n",
    "    y[tid] = x1[tid] + a * x2[tid];\n",
    "}\n",
    "''', 'my_func')\n",
    "x1 = cp.arange(25, dtype=cp.complex64).reshape(5, 5)\n",
    "x2 = 1j*cp.arange(25, dtype=cp.complex64).reshape(5, 5)\n",
    "y = cp.zeros((5, 5), dtype=cp.complex64)\n",
    "complex_kernel((5,), (5,), (x1, x2, y, cp.float32(2.0)))  # grid, block and arguments\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e23f775-216f-4046-b59f-92e93a5c5953",
   "metadata": {},
   "source": [
    "|CuPy/NumPy type|Corresponding kernel types|itemsize (bytes)|\n",
    "|--|--|--|\n",
    "|bool|bool|1|\n",
    "|int8|char, signed char|1|\n",
    "|int16|short, signed short|2|\n",
    "|int32|int, signed int|4|\n",
    "|int64|long long, signed long long|8|\n",
    "|uint8|unsigned char|1|\n",
    "|uint16|unsigned short|2|\n",
    "|uint32|unsigned int|4|\n",
    "|uint64|unsigned long long|8|\n",
    "|float16|half|2|\n",
    "|float32|float|4|\n",
    "|float64|double|8|\n",
    "|complex64|float2, cuFloatComplex, complex<float>|8|\n",
    "|complex128|double2, cuDoubleComplex, complex<double>|16|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8da9d604-14d3-40e4-a7fa-48b08b77afe6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(0.869101, 0.06029943, 0.5127114)]\n",
      "[(42., 0.06029943, 0.5127114)]\n"
     ]
    }
   ],
   "source": [
    "names = ['x', 'y', 'z']\n",
    "types = [np.float32]*3\n",
    "float3 = np.dtype({'names': names, 'formats': types})\n",
    "arg = np.random.rand(3).astype(np.float32).view(float3)\n",
    "print(arg)\n",
    "arg['x'] = 42.0\n",
    "print(arg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "854593b5-58ae-4d9a-b0ec-5682877fbd50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    }
   ],
   "source": [
    "float5x5 = np.dtype({'names': ['dummy'], 'formats': [(np.float32,(5,5))]})\n",
    "arg = np.random.rand(25).astype(np.float32).view(float5x5)\n",
    "print(arg.itemsize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "eefacdc4-6d30-4bec-9d7c-88cf6f219aa7",
   "metadata": {},
   "outputs": [],
   "source": [
    "loaded_from_source = r'''\n",
    "extern \"C\"{\n",
    "\n",
    "__global__ void test_sum(const float* x1, const float* x2, float* y, \\\n",
    "                         unsigned int N)\n",
    "{\n",
    "    unsigned int tid = blockDim.x * blockIdx.x + threadIdx.x;\n",
    "    if (tid < N)\n",
    "    {\n",
    "        y[tid] = x1[tid] + x2[tid];\n",
    "    }\n",
    "}\n",
    "\n",
    "__global__ void test_multiply(const float* x1, const float* x2, float* y, \\\n",
    "                              unsigned int N)\n",
    "{\n",
    "    unsigned int tid = blockDim.x * blockIdx.x + threadIdx.x;\n",
    "    if (tid < N)\n",
    "    {\n",
    "        y[tid] = x1[tid] * x2[tid];\n",
    "    }\n",
    "}\n",
    "\n",
    "}'''\n",
    "module = cp.RawModule(code=loaded_from_source)\n",
    "ker_sum = module.get_function('test_sum')\n",
    "ker_times = module.get_function('test_multiply')\n",
    "N = 10\n",
    "x1 = cp.arange(N**2, dtype=cp.float32).reshape(N, N)\n",
    "x2 = cp.ones((N, N), dtype=cp.float32)\n",
    "y = cp.zeros((N, N), dtype=cp.float32)\n",
    "ker_sum((N,), (N,), (x1, x2, y, N**2))   # y = x1 + x2\n",
    "assert cp.allclose(y, x1 + x2)\n",
    "ker_times((N,), (N,), (x1, x2, y, N**2)) # y = x1 * x2\n",
    "assert cp.allclose(y, x1 * x2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ca0b1ddf-8483-44ff-b055-e0b09159eea7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  3.,  6.,  9., 12., 15., 18., 21., 24., 27.], dtype=float32)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code = r'''\n",
    "template<typename T>\n",
    "__global__ void fx3(T* arr, int N) {\n",
    "    unsigned int tid = blockIdx.x * blockDim.x + threadIdx.x;\n",
    "    if (tid < N) {\n",
    "        arr[tid] = arr[tid] * 3;\n",
    "    }\n",
    "}\n",
    "'''\n",
    "\n",
    "name_exp = ['fx3<float>', 'fx3<double>']\n",
    "mod = cp.RawModule(code=code, options=('-std=c++11',),\n",
    "    name_expressions=name_exp)\n",
    "ker_float = mod.get_function(name_exp[0])  # compilation happens here\n",
    "N=10\n",
    "a = cp.arange(N, dtype=cp.float32)\n",
    "ker_float((1,), (N,), (a, N))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "42e1770a-c6b2-4a4a-b8f4-8fc6330284eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  3.,  6.,  9., 12., 15., 18., 21., 24., 27.])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ker_double = mod.get_function(name_exp[1])\n",
    "a = cp.arange(N, dtype=cp.float64)\n",
    "ker_double((1,), (N,), (a, N))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4d6b74a7-6960-463b-a688-e659dbf576d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "@cp.fuse()\n",
    "def squared_diff(x, y):\n",
    "    return (x - y) * (x - y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "aba69aef-448f-436c-b5e1-cf681b2539c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([81, 49, 25,  9,  1,  1,  9, 25, 49, 81])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_cp = cp.arange(10)\n",
    "y_cp = cp.arange(10)[::-1]\n",
    "squared_diff(x_cp, y_cp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "7638f003-c1de-48e0-80ac-8feb11c66afc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([81, 49, 25,  9,  1,  1,  9, 25, 49, 81])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_np = np.arange(10)\n",
    "y_np = np.arange(10)[::-1]\n",
    "squared_diff(x_np, y_np)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e4372931-c47d-4d32-8f44-e62db49abfc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "@cp.fuse()\n",
    "def sum_of_products(x, y):\n",
    "    return cp.sum(x * y, axis = -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "27415974-d14b-4718-b165-a09fbb9b8728",
   "metadata": {},
   "outputs": [],
   "source": [
    "@cp.fuse(kernel_name='squared_diff')\n",
    "def squared_diff(x, y):\n",
    "    return (x - y) * (x - y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "02b8efb0-5403-46d4-9cac-98ec9df7f1c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_np = np.arange(10)\n",
    "s = cp.cuda.Stream()\n",
    "with s:\n",
    "    a_cp = cp.asarray(a_np)  # H2D transfer on stream s\n",
    "    b_cp = cp.sum(a_cp)      # kernel launched on stream s\n",
    "    assert s == cp.cuda.get_current_stream()\n",
    "\n",
    "# fall back to the previous stream in use (here the default stream)\n",
    "# when going out of the scope of s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7f835a30-f6bf-463f-8a7e-c7e09198408c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Stream 323007184 (device 0)>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = cp.cuda.Stream()\n",
    "s.use()  # any subsequent operations are done on steam s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "bb589759-30cd-4ded-a766-b13819ecca3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Stream 0 (device -1)>"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b_np = cp.asnumpy(b_cp)\n",
    "assert s == cp.cuda.get_current_stream()\n",
    "cp.cuda.Stream.null.use()  # fall back to the default (null) stream"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "95e52a59-6a52-43aa-90b8-d326f46cd66e",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert cp.cuda.Stream.null == cp.cuda.get_current_stream()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "a3167376-fcf5-4d98-aea5-9c62873359cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "e1 = cp.cuda.Event()\n",
    "e1.record()\n",
    "a_cp = b_cp * a_cp + 8\n",
    "e2 = cp.cuda.get_current_stream().record()\n",
    "\n",
    "# set up a stream order\n",
    "s2 = cp.cuda.Stream()\n",
    "s2.wait_event(e2)\n",
    "with s2:\n",
    "    # the a_cp is guaranteed updated when this copy (on s2) starts\n",
    "    a_np = cp.asnumpy(a_cp)\n",
    "\n",
    "# timing\n",
    "e2.synchronize()\n",
    "t = cp.cuda.get_elapsed_time(e1, e2)  # only include the compute time, not the copy time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "0bd61dcb-bc2f-41ce-8e5c-7cd43f9380a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cupyx.scipy.fft as cufft\n",
    "import scipy.fft\n",
    "\n",
    "a = cp.random.random(100).astype(cp.complex64)\n",
    "with scipy.fft.set_backend(cufft):\n",
    "    b = scipy.fft.fft(a)  # equivalent to cufft.fft(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "993476dc-3059-433a-be0e-d8eac34c4ef9",
   "metadata": {},
   "outputs": [],
   "source": [
    "scipy.fft.set_global_backend(cufft)\n",
    "\n",
    "a = cp.random.random(100).astype(cp.complex64)\n",
    "b = scipy.fft.fft(a)  # equivalent to cufft.fft(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "d364b112-2cc6-42c0-8370-6f87fdc8a2e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from cupyx.scipy.fft import get_fft_plan\n",
    "\n",
    "a = cp.random.random((4, 64, 64)).astype(cp.complex64)\n",
    "plan = get_fft_plan(a, axes=(1, 2), value_type='C2C')  # for batched, C2C, 2D transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "93b53266-53e5-44aa-88fe-246ad3790870",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cupyx.scipy.fft\n",
    "\n",
    "# the rest of the arguments must match those used when generating the plan\n",
    "out = cupyx.scipy.fft.fft2(a, axes=(1, 2), plan=plan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "40cb4be2-253e-4a97-9901-ee4f6ccdcad7",
   "metadata": {},
   "outputs": [],
   "source": [
    "with plan:\n",
    "    # the arguments must match those used when generating the plan\n",
    "    out = cp.fft.fft2(a, axes=(1, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "0c2d1aa3-f209-4ec5-bf99-3c0143a9a9b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------- cuFFT plan cache (device 0) -------------------\n",
      "cache enabled? True\n",
      "current / max size   : 1 / 16 (counts)\n",
      "current / max memsize: 512 / (unlimited) (bytes)\n",
      "hits / misses: 1 / 2 (counts)\n",
      "\n",
      "cached plans (most recently used first):\n",
      "key: (100, 41, 1, None), plan type: Plan1d, memory usage: 512\n",
      "\n"
     ]
    }
   ],
   "source": [
    "cache = cp.fft.config.get_plan_cache()\n",
    "cache.show_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "af2ebb34-fe56-4156-83e9-986375c8f2d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------- cuFFT plan cache (device 0) -------------------\n",
      "cache enabled? True\n",
      "current / max size   : 2 / 16 (counts)\n",
      "current / max memsize: 1024 / (unlimited) (bytes)\n",
      "hits / misses: 1 / 3 (counts)\n",
      "\n",
      "cached plans (most recently used first):\n",
      "key: ((64, 64), (64, 64), 1, 4096, (64, 64), 1, 4096, 105, 4, 'C', 2, None), plan type: PlanNd, memory usage: 512\n",
      "key: (100, 41, 1, None), plan type: Plan1d, memory usage: 512\n",
      "\n"
     ]
    }
   ],
   "source": [
    "a = cp.random.random((4, 64, 64))\n",
    "out = cp.fft.fftn(a, axes=(1, 2))\n",
    "cache.show_info()  # hit = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "d2384ab9-65f0-4b74-aebe-a480be9ed2f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------- cuFFT plan cache (device 0) -------------------\n",
      "cache enabled? True\n",
      "current / max size   : 2 / 16 (counts)\n",
      "current / max memsize: 1024 / (unlimited) (bytes)\n",
      "hits / misses: 2 / 3 (counts)\n",
      "\n",
      "cached plans (most recently used first):\n",
      "key: ((64, 64), (64, 64), 1, 4096, (64, 64), 1, 4096, 105, 4, 'C', 2, None), plan type: PlanNd, memory usage: 512\n",
      "key: (100, 41, 1, None), plan type: Plan1d, memory usage: 512\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# perform the same transform again, the plan is looked up from cache and reused\n",
    "out = cp.fft.fftn(a, axes=(1, 2))\n",
    "cache.show_info()  # hit = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "4c467c58-09dd-4411-9b0d-1f7507e066d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=============== cuFFT plan cache info (all devices) ===============\n",
      "------------------- cuFFT plan cache (device 0) -------------------\n",
      "cache enabled? True\n",
      "current / max size   : 0 / 16 (counts)\n",
      "current / max memsize: 0 / (unlimited) (bytes)\n",
      "hits / misses: 0 / 0 (counts)\n",
      "\n",
      "cached plans (most recently used first):\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# clear the cache\n",
    "cache.clear()\n",
    "cp.fft.config.show_plan_cache_info()  # = cache.show_info(), for all devices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "736e55e3-5797-4625-8dbd-2377bf132f27",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "400\n",
      "407040\n",
      "1197568\n",
      "0\n",
      "400\n",
      "276480\n",
      "1197568\n",
      "0\n",
      "275968\n",
      "1197568\n",
      "0\n",
      "275968\n",
      "276480\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "mempool = cupy.get_default_memory_pool()\n",
    "pinned_mempool = cupy.get_default_pinned_memory_pool()\n",
    "\n",
    "# Create an array on CPU.\n",
    "# NumPy allocates 400 bytes in CPU (not managed by CuPy memory pool).\n",
    "a_cpu = np.ndarray(100, dtype=np.float32)\n",
    "print(a_cpu.nbytes)                      # 400\n",
    "\n",
    "# You can access statistics of these memory pools.\n",
    "print(mempool.used_bytes())              # 0\n",
    "print(mempool.total_bytes())             # 0\n",
    "print(pinned_mempool.n_free_blocks())    # 0\n",
    "\n",
    "# Transfer the array from CPU to GPU.\n",
    "# This allocates 400 bytes from the device memory pool, and another 400\n",
    "# bytes from the pinned memory pool.  The allocated pinned memory will be\n",
    "# released just after the transfer is complete.  Note that the actual\n",
    "# allocation size may be rounded to larger value than the requested size\n",
    "# for performance.\n",
    "a = cupy.array(a_cpu)\n",
    "print(a.nbytes)                          # 400\n",
    "print(mempool.used_bytes())              # 512\n",
    "print(mempool.total_bytes())             # 512\n",
    "print(pinned_mempool.n_free_blocks())    # 1\n",
    "\n",
    "# When the array goes out of scope, the allocated device memory is released\n",
    "# and kept in the pool for future reuse.\n",
    "a = None  # (or `del a`)\n",
    "print(mempool.used_bytes())              # 0\n",
    "print(mempool.total_bytes())             # 512\n",
    "print(pinned_mempool.n_free_blocks())    # 1\n",
    "\n",
    "# You can clear the memory pool by calling `free_all_blocks`.\n",
    "mempool.free_all_blocks()\n",
    "pinned_mempool.free_all_blocks()\n",
    "print(mempool.used_bytes())              # 0\n",
    "print(mempool.total_bytes())             # 0\n",
    "print(pinned_mempool.n_free_blocks())    # 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "3b2021c1-3f4c-4319-a8ac-60f2e260c3fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "# Set the hard-limit to 1 GiB:\n",
    "#   $ export CUPY_GPU_MEMORY_LIMIT=\"1073741824\"\n",
    "\n",
    "# You can also specify the limit in fraction of the total amount of memory\n",
    "# on the GPU. If you have a GPU with 2 GiB memory, the following is\n",
    "# equivalent to the above configuration.\n",
    "#   $ export CUPY_GPU_MEMORY_LIMIT=\"50%\"\n",
    "\n",
    "print(cupy.get_default_memory_pool().get_limit())  # 1073741824"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "a1334cd2-67cc-414c-91f4-265bfb89d553",
   "metadata": {},
   "outputs": [],
   "source": [
    "mempool = cupy.get_default_memory_pool()\n",
    "\n",
    "with cupy.cuda.Device(0):\n",
    "    mempool.set_limit(size=2*1024**3)  # 2 GiB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "dae1ad36-4690-4f98-ad88-8af51211aa11",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Use managed memory\n",
    "cupy.cuda.set_allocator(cupy.cuda.MemoryPool(cupy.cuda.malloc_managed).malloc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "626fb90c-ac49-4394-ab93-b11672033c09",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Disable memory pool for device memory (GPU)\n",
    "cupy.cuda.set_allocator(None)\n",
    "\n",
    "# Disable memory pool for pinned memory (CPU).\n",
    "cupy.cuda.set_pinned_memory_allocator(None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "cef1c3fa-b2b2-4660-8ad5-c60919920b5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my_func             :    CPU:  3477.270 us   +/- 184.884 (min:  3290.066 / max:  3983.701) us     GPU-0:  3492.653 us   +/- 193.207 (min:  3298.752 / max:  4030.816) us\n"
     ]
    }
   ],
   "source": [
    "from cupyx.profiler import benchmark\n",
    "\n",
    "def my_func(a):\n",
    "    return cp.sqrt(cp.sum(a**2, axis=-1))\n",
    "\n",
    "a = cp.random.random((256, 1024))\n",
    "print(benchmark(my_func, (a,), n_repeat=20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "e4d0447c-bed5-448b-8ca6-7ff316dd05a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "start_gpu = cp.cuda.Event()\n",
    "end_gpu = cp.cuda.Event()\n",
    "\n",
    "start_gpu.record()\n",
    "start_cpu = time.perf_counter()\n",
    "out = my_func(a)\n",
    "end_cpu = time.perf_counter()\n",
    "end_gpu.record()\n",
    "end_gpu.synchronize()\n",
    "t_gpu = cp.cuda.get_elapsed_time(start_gpu, end_gpu)\n",
    "t_cpu = end_cpu - start_cpu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "8b3c45c8-f31d-4e2d-8600-a4540dc1ede7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum                 :    CPU:  1910.008 us   +/- 34.493 (min:  1878.246 / max:  2108.537) us     GPU-0:  1919.351 us   +/- 40.884 (min:  1883.200 / max:  2158.592) us\n"
     ]
    }
   ],
   "source": [
    "from cupyx.profiler import benchmark\n",
    "a = cp.random.random((256, 256, 256), dtype=cp.float32)\n",
    "print(benchmark(a.sum, (), n_repeat=100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "c655565b-6385-410f-b59a-7155dd41cd15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum                 :    CPU:  1895.321 us   +/- 32.398 (min:  1878.139 / max:  2056.718) us     GPU-0:  1903.077 us   +/- 38.512 (min:  1883.232 / max:  2088.416) us\n"
     ]
    }
   ],
   "source": [
    "print(benchmark(a.sum, (), n_repeat=100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "b7b0145e-2c1f-4137-8b2c-9885529530e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'cupy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "arr = cupy.random.randn(1, 2, 3, 4).astype(cupy.float32)\n",
    "result = np.sum(arr)\n",
    "print(type(result))  # => <class 'cupy._core.core.ndarray'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "554021b8-1de8-451b-b25b-869caf63ef9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a CuPy array.\n",
    "cx1 = cupy.random.randn(1, 2, 3, 4).astype(cupy.float32)\n",
    "\n",
    "# Convert it into a DLPack tensor.\n",
    "dx = cx1.toDlpack()\n",
    "\n",
    "# Convert it back to a CuPy array.\n",
    "cx2 = cupy.from_dlpack(dx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "a7de26d8-8a90-462f-a790-cde2d112f8be",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a memory chunk from raw pointer and its size.\n",
    "mem = cupy.cuda.UnownedMemory(140359025819648, 1024, owner=None)\n",
    "\n",
    "# Wrap it as a MemoryPointer.\n",
    "memptr = cupy.cuda.MemoryPointer(mem, offset=0)\n",
    "\n",
    "# Create an ndarray view backed by the memory pointer.\n",
    "arr = cupy.ndarray((16, 16), dtype=cupy.float32, memptr=memptr)\n",
    "assert arr.nbytes <= arr.data.mem.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "0a57b15d-b8b2-49c2-afe5-409190713129",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21493199360 80\n"
     ]
    }
   ],
   "source": [
    "arr = cupy.arange(10)\n",
    "print(arr.data.ptr, arr.nbytes)  # => (140359025819648, 80)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "900ee782-050c-41cc-a808-458518b0caeb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "324890352 0\n"
     ]
    }
   ],
   "source": [
    "s = cupy.cuda.Stream()\n",
    "print(s.ptr, s.device_id)  # => (93997451352336, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "ee42940b-d794-4eaf-b064-572558ae5e1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4294967295], dtype=uint32)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([-1], dtype=np.float32).astype(np.uint32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "2cc7635c-1eb0-4951-93eb-548f99f36eed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0], dtype=uint32)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cupy.array([-1], dtype=np.float32).astype(np.uint32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "395211ce-d28c-4ad8-8f9d-7406fe41ea5b",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "randn() got an unexpected keyword argument 'dtype'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mTypeError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[69]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mnp\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrandom\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrandn\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m=\u001b[49m\u001b[43mnp\u001b[49m\u001b[43m.\u001b[49m\u001b[43mfloat32\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[31mTypeError\u001b[39m: randn() got an unexpected keyword argument 'dtype'"
     ]
    }
   ],
   "source": [
    "np.random.randn(dtype=np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72573f09-9059-4584-a164-b428c6e651d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.random.randn(dtype=np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0978da42-1e81-4b93-aad2-ae462c4e3132",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([0, 1, 2])\n",
    "x[[1, 3]] = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8926f9a7-ea11-4244-abd8-d6013036fe52",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = cupy.array([0, 1, 2])\n",
    "x[[1, 3]] = 10\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fd53b2c-820a-4b8b-9bd3-6a52582dbd79",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = cupy.zeros((2,))\n",
    "i = cupy.arange(10000) % 2\n",
    "v = cupy.arange(10000).astype(np.float32)\n",
    "a[i] = v\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b12aff7a-eb23-45ed-8bbc-9eba9d18c771",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_cpu = np.zeros((2,))\n",
    "i_cpu = np.arange(10000) % 2\n",
    "v_cpu = np.arange(10000).astype(np.float32)\n",
    "a_cpu[i_cpu] = v_cpu\n",
    "a_cpu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5478dd14-b991-4e70-98e0-89060e83fb32",
   "metadata": {},
   "outputs": [],
   "source": [
    "type(np.sum(np.arange(3))) == np.int64"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3539a510-274c-4d65-bad2-7f8ab93d1118",
   "metadata": {},
   "outputs": [],
   "source": [
    "type(cupy.sum(cupy.arange(3))) == cupy.ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4b36c21-d270-45ee-a2b5-92033b64f3a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "(np.array(3, dtype=np.int32) * np.array([1., 2.], dtype=np.float32)).dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0d2a95a-5ab4-4a3c-9ab1-00ff48853ed7",
   "metadata": {},
   "outputs": [],
   "source": [
    "(np.array(300000, dtype=np.int32) * np.array([1., 2.], dtype=np.float32)).dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea751dbc-2a06-4166-ba97-7f3b07d0dd6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "(cupy.array(3, dtype=np.int32) * cupy.array([1., 2.], dtype=np.float32)).dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfbf2637-9932-4e98-ada9-f093ea27bb47",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.power([np.arange(5)], 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4648e0de-d49c-47c0-9497-90ce3c32aaa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.power([cupy.arange(5)], 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b84ebd8-c49e-4635-a973-0e9153ab5a16",
   "metadata": {},
   "outputs": [],
   "source": [
    "seed = np.array([1, 2, 3, 4, 5])\n",
    "rs = cupy.random.RandomState(seed=seed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b26aac4-c06f-423d-a1ac-85bf737d087a",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [0.5 + 3.7j, complex(0.7, np.nan), complex(np.nan, -3.9), complex(np.nan, np.nan)]\n",
    "a_np = np.asarray(a)\n",
    "print(a_np.max(), a_np.min())\n",
    "a_cp = cp.asarray(a_np)\n",
    "print(a_cp.max(), a_cp.min())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efda0caa-d106-41b2-a502-9ed19901c956",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1, 2], [3, 4]], order='F')\n",
    "print((a + a).flags.f_contiguous)\n",
    "a = cp.array([[1, 2], [3, 4]], order='F')\n",
    "print((a + a).flags.f_contiguous)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c994beb1-9767-436d-bda5-d57078a5183d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# class cupy.ndarray(self, shape, dtype=float, memptr=None, strides=None, order='C')\n",
    "a = cupy.arange(3)\n",
    "a[[1, 3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b44976d-5282-4120-a612-11cd28add528",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = cupy.arange(3)\n",
    "x[[1, 3]] = 10\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3bbc52e-10fa-475a-8f8a-7dbfee7d1a54",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = cupy.zeros((2,))\n",
    "i = cupy.arange(10000) % 2\n",
    "v = cupy.arange(10000).astype(cupy.float64)\n",
    "a[i] = v\n",
    "a  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7607e60-6056-4351-b9f3-f49e9a5bbb0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_cpu = np.zeros((2,))\n",
    "i_cpu = np.arange(10000) % 2\n",
    "v_cpu = np.arange(10000).astype(np.float64)\n",
    "a_cpu[i_cpu] = v_cpu\n",
    "a_cpu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "595c0b20-74f4-4567-9390-7130e39b952e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.array(obj, dtype=None, copy=True, order='K', subok=False, ndmin=0, *, blocking=False)\n",
    "# cupy.asarray(a, dtype=None, order=None, *, blocking=False)\n",
    "# cupy.asnumpy(a, stream=None, order='C', out=None, *, blocking=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7330de9f-c353-45ca-bb6f-8b1e1db8310c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.get_array_module(*args)\n",
    "def softplus(x):\n",
    "    xp = cupy.get_array_module(x)\n",
    "    return xp.maximum(0, x) + xp.log1p(xp.exp(-abs(x)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a32397-c7bc-47a8-9a64-561cf1665f49",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupyx.scipy.get_array_module(*args)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ce4ef76-e7ed-44bb-9e49-71deea15d37d",
   "metadata": {},
   "source": [
    "# Math operations\n",
    "\n",
    "|||\n",
    "|--|--|\n",
    "|add(x1, x2, /[, out, casting, dtype])|Adds two arrays elementwise.|\n",
    "|subtract(x1, x2, /[, out, casting, dtype])|Subtracts arguments elementwise.|\n",
    "|multiply(x1, x2, /[, out, casting, dtype])|Multiplies two arrays elementwise.|\n",
    "|matmul|matmul(x1, x2, /, out=None, **kwargs)|\n",
    "|divide|true_divide(x1, x2, /, out=None, *, casting='same_kind', dtype=None)|\n",
    "|logaddexp(x1, x2, /[, out, casting, dtype])|Computes log(exp(x1) + exp(x2)) elementwise.|\n",
    "|logaddexp2(x1, x2, /[, out, casting, dtype])|Computes log2(exp2(x1) + exp2(x2)) elementwise.|\n",
    "|true_divide(x1, x2, /[, out, casting, dtype])|Elementwise true division (i.e.|\n",
    "|floor_divide(x1, x2, /[, out, casting, dtype])|Elementwise floor division (i.e.|\n",
    "|negative(x, /[, out, casting, dtype])|Takes numerical negative elementwise.|\n",
    "|positive(x, /[, out, casting, dtype])|Takes numerical positive elementwise.|\n",
    "|power(x1, x2, /[, out, casting, dtype])|Computes x1 ** x2 elementwise.|\n",
    "|float_power(x1, x2, /[, out, casting, dtype])|First array elements raised to powers from second array, element-wise.|\n",
    "|remainder|mod(x1, x2, /, out=None, *, casting='same_kind', dtype=None)|\n",
    "|mod(x1, x2, /[, out, casting, dtype])|Computes the remainder of Python division elementwise.|\n",
    "|fmod(x1, x2, /[, out, casting, dtype])|Computes the remainder of C division elementwise.|\n",
    "|divmod(x1, x2[, out1, out2], / [[, out, ...])||\n",
    "|absolute(x, /[, out, casting, dtype])|Elementwise absolute value function.|\n",
    "|fabs(x, /[, out, casting, dtype])|Calculates absolute values element-wise.|\n",
    "|rint(x, /[, out, casting, dtype])|Rounds each element of an array to the nearest integer.|\n",
    "|sign(x, /[, out, casting, dtype])|Elementwise sign function.|\n",
    "|heaviside(x1, x2, /[, out, casting, dtype])|Compute the Heaviside step function.|\n",
    "|conj|conjugate(x, /, out=None, *, casting='same_kind', dtype=None)|\n",
    "|conjugate(x, /[, out, casting, dtype])|Returns the complex conjugate, element-wise.|\n",
    "|exp(x, /[, out, casting, dtype])|Elementwise exponential function.|\n",
    "|exp2(x, /[, out, casting, dtype])|Elementwise exponentiation with base 2.|\n",
    "|log(x, /[, out, casting, dtype])|Elementwise natural logarithm function.|\n",
    "|log2(x, /[, out, casting, dtype])|Elementwise binary logarithm function.|\n",
    "|log10(x, /[, out, casting, dtype])|Elementwise common logarithm function.|\n",
    "|expm1(x, /[, out, casting, dtype])|Computes exp(x) - 1 elementwise.|\n",
    "|log1p(x, /[, out, casting, dtype])|Computes log(1 + x) elementwise.|\n",
    "|sqrt(x, /[, out, casting, dtype])|Elementwise square root function.|\n",
    "|square(x, /[, out, casting, dtype])|Elementwise square function.|\n",
    "|cbrt(x, /[, out, casting, dtype])|Elementwise cube root function.|\n",
    "|reciprocal(x, /[, out, casting, dtype])|Computes 1 / x elementwise.|\n",
    "|gcd(x1, x2, /[, out, casting, dtype])|Computes gcd of x1 and x2 elementwise.|\n",
    "|lcm(x1, x2, /[, out, casting, dtype])|Computes lcm of x1 and x2 elementwise.|\n",
    "\n",
    "# Trigonometric functions\n",
    "\n",
    "|||\n",
    "|--|--|\n",
    "|sin(x, /[, out, casting, dtype])|Elementwise sine function.|\n",
    "|cos(x, /[, out, casting, dtype])|Elementwise cosine function.|\n",
    "|tan(x, /[, out, casting, dtype])|Elementwise tangent function.|\n",
    "|arcsin(x, /[, out, casting, dtype])|Elementwise inverse-sine function (a.k.a.|\n",
    "|arccos(x, /[, out, casting, dtype])|Elementwise inverse-cosine function (a.k.a.|\n",
    "|arctan(x, /[, out, casting, dtype])|Elementwise inverse-tangent function (a.k.a.|\n",
    "|arctan2(x1, x2, /[, out, casting, dtype])|Elementwise inverse-tangent of the ratio of two arrays.|\n",
    "|hypot(x1, x2, /[, out, casting, dtype])|Computes the hypoteneous of orthogonal vectors of given length.|\n",
    "|sinh(x, /[, out, casting, dtype])|Elementwise hyperbolic sine function.|\n",
    "|cosh(x, /[, out, casting, dtype])|Elementwise hyperbolic cosine function.|\n",
    "|tanh(x, /[, out, casting, dtype])|Elementwise hyperbolic tangent function.|\n",
    "|arcsinh(x, /[, out, casting, dtype])|Elementwise inverse of hyperbolic sine function.|\n",
    "|arccosh(x, /[, out, casting, dtype])|Elementwise inverse of hyperbolic cosine function.|\n",
    "|arctanh(x, /[, out, casting, dtype])|Elementwise inverse of hyperbolic tangent function.|\n",
    "|degrees|rad2deg(x, /, out=None, *, casting='same_kind', dtype=None)|\n",
    "|radians(x, /[, out, casting, dtype])|Converts angles from degrees to radians elementwise.|\n",
    "|deg2rad|radians(x, /, out=None, *, casting='same_kind', dtype=None)|\n",
    "|rad2deg(x, /[, out, casting, dtype])|Converts angles from radians to degrees elementwise.|\n",
    "\n",
    "# Bit-twiddling functions\n",
    "\n",
    "|||\n",
    "|--|--|\n",
    "|bitwise_and(x1, x2, /[, out, casting, dtype])|Computes the bitwise AND of two arrays elementwise.|\n",
    "|bitwise_or(x1, x2, /[, out, casting, dtype])|Computes the bitwise OR of two arrays elementwise.|\n",
    "|bitwise_xor(x1, x2, /[, out, casting, dtype])|Computes the bitwise XOR of two arrays elementwise.|\n",
    "|invert(x, /[, out, casting, dtype])|Computes the bitwise NOT of an array elementwise.|\n",
    "|left_shift(x1, x2, /[, out, casting, dtype])|Shifts the bits of each integer element to the left.|\n",
    "|right_shift(x1, x2, /[, out, casting, dtype])|Shifts the bits of each integer element to the right.|\n",
    "\n",
    "# Comparison functions\n",
    "\n",
    "|||\n",
    "|--|--|\n",
    "|greater(x1, x2, /[, out, casting, dtype])|Tests elementwise if x1 > x2.|\n",
    "|greater_equal(x1, x2, /[, out, casting, dtype])|Tests elementwise if x1 >= x2.|\n",
    "|less(x1, x2, /[, out, casting, dtype])|Tests elementwise if x1 < x2.|\n",
    "|less_equal(x1, x2, /[, out, casting, dtype])|Tests elementwise if x1 <= x2.|\n",
    "|not_equal(x1, x2, /[, out, casting, dtype])|Tests elementwise if x1 != x2.|\n",
    "|equal(x1, x2, /[, out, casting, dtype])|Tests elementwise if x1 == x2.|\n",
    "|logical_and(x1, x2, /[, out, casting, dtype])|Computes the logical AND of two arrays.|\n",
    "|logical_or(x1, x2, /[, out, casting, dtype])|Computes the logical OR of two arrays.|\n",
    "|logical_xor(x1, x2, /[, out, casting, dtype])|Computes the logical XOR of two arrays.|\n",
    "|logical_not(x, /[, out, casting, dtype])|Computes the logical NOT of an array.|\n",
    "|maximum(x1, x2, /[, out, casting, dtype])|Takes the maximum of two arrays elementwise.|\n",
    "|minimum(x1, x2, /[, out, casting, dtype])|Takes the minimum of two arrays elementwise.|\n",
    "|fmax(x1, x2, /[, out, casting, dtype])|Takes the maximum of two arrays elementwise.|\n",
    "|fmin(x1, x2, /[, out, casting, dtype])|Takes the minimum of two arrays elementwise.|\n",
    "\n",
    "# Floating functions\n",
    "\n",
    "|||\n",
    "|--|--|\n",
    "|isfinite(x, /[, out, casting, dtype])|Tests finiteness elementwise.|\n",
    "|isinf(x, /[, out, casting, dtype])|Tests if each element is the positive or negative infinity.|\n",
    "|isnan(x, /[, out, casting, dtype])|Tests if each element is a NaN.|\n",
    "|fabs(x, /[, out, casting, dtype])|Calculates absolute values element-wise.|\n",
    "|signbit(x, /[, out, casting, dtype])|Tests elementwise if the sign bit is set (i.e.|\n",
    "|copysign(x1, x2, /[, out, casting, dtype])|Returns the first argument with the sign bit of the second elementwise.|\n",
    "|nextafter(x1, x2, /[, out, casting, dtype])|Computes the nearest neighbor float values towards the second argument.|\n",
    "|modf(x[, out1, out2], / [[, out, casting, dtype])|Extracts the fractional and integral parts of an array elementwise.|\n",
    "|ldexp(x1, x2, /[, out, casting, dtype])|Computes x1 * 2 ** x2 elementwise.|\n",
    "|frexp(x[, out1, out2], / [[, out, casting, ...])|Decomposes each element to mantissa and two's exponent.|\n",
    "|fmod(x1, x2, /[, out, casting, dtype])|Computes the remainder of C division elementwise.|\n",
    "|floor(x, /[, out, casting, dtype])|Rounds each element of an array to its floor integer.|\n",
    "|ceil(x, /[, out, casting, dtype])|Rounds each element of an array to its ceiling integer.|\n",
    "|trunc(x, /[, out, casting, dtype])|Rounds each element of an array towards zero.|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef5a6b84-caaf-44c1-b6a9-948353375d12",
   "metadata": {},
   "outputs": [],
   "source": [
    "# class cupy.ufunc(name, nin, nout, _Ops ops, preamble=u'', loop_prep=u'', doc=u'', default_casting=None, _Ops out_ops=None, *, cutensor_op=None, scatter_op=None)\n",
    "# cupy.add(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.subtract(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.multiply(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.matmul = <cupy._core._gufuncs._GUFunc object>\n",
    "#   matmul(x1, x2, /, out=None, **kwargs)\n",
    "# cupy.divide()\n",
    "#   true_divide(x1, x2, /, out=None, *, casting=’same_kind’, dtype=None)\n",
    "# cupy.logaddexp(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.logaddexp2(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.true_divide(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.floor_divide(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.negative(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.positive(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.power(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   Computes x1 ** x2 elementwise.\n",
    "# cupy.float_power(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.remainder()\n",
    "#   mod(x1, x2, /, out=None, *, casting=’same_kind’, dtype=None)\n",
    "# cupy.mod(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.fmod(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.divmod(x1, x2, [out1, out2, ]/, [out=(None, None), ]\\*, casting='same_kind', dtype=None)\n",
    "# cupy.absolute(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.fabs(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.rint(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.sign(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.heaviside(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.conj()\n",
    "#   conjugate(x, /, out=None, *, casting=’same_kind’, dtype=None)\n",
    "# cupy.conjugate(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.exp(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.exp2(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.log(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.log2(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.log10(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.expm1(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   Computes exp(x) - 1 elementwise.\n",
    "# cupy.log1p(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   Computes log(1 + x) elementwise.\n",
    "# cupy.sqrt(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.square(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.cbrt(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.reciprocal(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   Computes 1 / x elementwise.\n",
    "# cupy.gcd(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.lcm(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.sin(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.cos(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.tan(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.arcsin(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.arccos(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.arctan(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.arctan2(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.hypot(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   This is equivalent to sqrt(x1 **2 + x2 ** 2), while this function is more efficient.\n",
    "# cupy.sinh(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.cosh(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.tanh(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.arcsinh(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.arccosh(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.arctanh(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.degrees()\n",
    "#   rad2deg(x, /, out=None, *, casting=’same_kind’, dtype=None)\n",
    "# cupy.radians(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.deg2rad()\n",
    "#   radians(x, /, out=None, *, casting=’same_kind’, dtype=None)\n",
    "# cupy.rad2deg(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.bitwise_and(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.bitwise_or(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.bitwise_xor(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.invert(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   cupy.bitwise_not() is an alias for cupy.invert()\n",
    "# cupy.left_shift(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.right_shift(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.greater(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   Tests elementwise if x1 > x2.\n",
    "# cupy.greater_equal(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   Tests elementwise if x1 >= x2.\n",
    "# cupy.less(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   Tests elementwise if x1 < x2.\n",
    "# cupy.less_equal(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   Tests elementwise if x1 <= x2.\n",
    "# cupy.not_equal(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   Tests elementwise if x1 != x2.\n",
    "# cupy.equal(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   Tests elementwise if x1 == x2.\n",
    "# cupy.logical_and(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.logical_or(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.logical_xor(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.logical_not(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.maximum(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.minimum(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.fmax(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.fmin(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.isfinite(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.isinf(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.isnan(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.signbit(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.copysign(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.nextafter(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.modf(x, [out1, out2, ]/, [out=(None, None), ]\\*, casting='same_kind', dtype=None)\n",
    "# cupy.ldexp(x1, x2, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "#   Computes x1 * 2 ** x2 elementwise.\n",
    "# cupy.frexp(x, [out1, out2, ]/, [out=(None, None), ]\\*, casting='same_kind', dtype=None)\n",
    "# cupy.floor(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.ceil(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.trunc(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# class cupyx.GeneralizedUFunc(func, signature, **kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e61fa17-d875-47a4-b017-163584c1235c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.empty(shape, dtype=<class 'float'>, order='C')\n",
    "# cupy.empty_like(prototype, dtype=None, order='K', subok=None, shape=None)\n",
    "# cupy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C')\n",
    "# cupy.identity(n, dtype=<class 'float'>)\n",
    "# cupy.ones(shape, dtype=<class 'float'>, order='C')\n",
    "# cupy.ones_like(a, dtype=None, order='K', subok=None, shape=None)\n",
    "# cupy.zeros(shape, dtype=<class 'float'>, order='C')\n",
    "# cupy.zeros_like(a, dtype=None, order='K', subok=None, shape=None)\n",
    "# cupy.full(shape, fill_value, dtype=None, order='C')\n",
    "# cupy.full_like(a, fill_value, dtype=None, order='K', subok=None, shape=None)\n",
    "# cupy.asanyarray(a, dtype=None, order=None, *, blocking=False)\n",
    "# cupy.ascontiguousarray(a, dtype=None)\n",
    "# cupy.copy(a, order='K')\n",
    "# cupy.frombuffer(*args, **kwargs)\n",
    "# cupy.fromfile(*args, **kwargs)\n",
    "# cupy.fromfunction(*args, **kwargs)\n",
    "# cupy.fromiter(*args, **kwargs)\n",
    "# cupy.fromstring(*args, **kwargs)\n",
    "# cupy.loadtxt(*args, **kwargs)\n",
    "# cupy.arange(start, stop=None, step=1, dtype=None)\n",
    "# cupy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)\n",
    "# cupy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)\n",
    "# cupy.meshgrid(*xi, **kwargs)\n",
    "# cupy.mgrid = <cupy._creation.ranges.nd_grid object>\n",
    "# cupy.ogrid = <cupy._creation.ranges.nd_grid object>\n",
    "# cupy.diag(v, k=0)\n",
    "# cupy.diagflat(v, k=0)\n",
    "# cupy.tri(N, M=None, k=0, dtype=<class 'float'>)\n",
    "# cupy.tril(m, k=0)\n",
    "# cupy.triu(m, k=0)\n",
    "# cupy.vander(x, N=None, increasing=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8abafd6e-8d1a-4210-b31e-c871c4f50ed0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.copyto(dst, src, casting='same_kind', where=None)\n",
    "# cupy.shape(a)\n",
    "# cupy.reshape(a, newshape, order='C')\n",
    "# cupy.ravel(a, order='C')\n",
    "# cupy.moveaxis(a, source, destination)\n",
    "# cupy.rollaxis(a, axis, start=0)\n",
    "# cupy.swapaxes(a, axis1, axis2)\n",
    "# cupy.transpose(a, axes=None)\n",
    "# cupy.atleast_1d(*arys)\n",
    "# cupy.atleast_2d(*arys)\n",
    "# cupy.atleast_3d(*arys)\n",
    "# class cupy.broadcast(*arrays)\n",
    "# cupy.broadcast_to(array, shape)\n",
    "# cupy.broadcast_arrays(*args)\n",
    "# cupy.expand_dims(a, axis)\n",
    "# cupy.squeeze(a, axis=None)\n",
    "# cupy.asfarray(a, dtype=<class 'numpy.float64'>)\n",
    "# cupy.asfortranarray(a, dtype=None)\n",
    "# cupy.asarray_chkfinite(a, dtype=None, order=None)\n",
    "# cupy.require(a, dtype=None, requirements=None)\n",
    "# cupy.concatenate(tup, axis=0, out=None, *, dtype=None, casting='same_kind')\n",
    "# cupy.stack(tup, axis=0, out=None, *, dtype=None, casting='same_kind')\n",
    "# cupy.vstack(tup, *, dtype=None, casting='same_kind')\n",
    "# cupy.hstack(tup, *, dtype=None, casting='same_kind')\n",
    "# cupy.dstack(tup)\n",
    "# cupy.column_stack(tup)\n",
    "# cupy.row_stack(tup, *, dtype=None, casting='same_kind')\n",
    "# cupy.split(ary, indices_or_sections, axis=0)\n",
    "# cupy.array_split(ary, indices_or_sections, axis=0)\n",
    "# cupy.dsplit(ary, indices_or_sections)\n",
    "# cupy.hsplit(ary, indices_or_sections)\n",
    "# cupy.vsplit(ary, indices_or_sections)\n",
    "# cupy.tile(A, reps)\n",
    "# cupy.repeat(a, repeats, axis=None)\n",
    "# cupy.delete(arr, indices, axis=None)\n",
    "# cupy.append(arr, values, axis=None)\n",
    "# cupy.resize(a, new_shape)\n",
    "# cupy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None, *, equal_nan=True)\n",
    "# cupy.trim_zeros(filt, trim='fb')\n",
    "# cupy.flip(a, axis=None)\n",
    "# cupy.fliplr(a)\n",
    "# cupy.flipud(a)\n",
    "# cupy.roll(a, shift, axis=None)\n",
    "# cupy.rot90(a, k=1, axes=(0, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "461c980f-c847-42d3-b205-612eb54bb152",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.packbits(a, axis=None, bitorder='big')\n",
    "# cupy.unpackbits(a, axis=None, bitorder='big')\n",
    "# cupy.binary_repr(num, width=None)\n",
    "# cupy.can_cast(from_, to, casting='safe')\n",
    "# cupy.min_scalar_type(a)\n",
    "# cupy.result_type(*arrays_and_dtypes)\n",
    "# cupy.common_type(*arrays)\n",
    "# cupy.fft.fft(a, n=None, axis=-1, norm=None)\n",
    "# cupy.fft.ifft(a, n=None, axis=-1, norm=None)\n",
    "# cupy.fft.fft2(a, s=None, axes=(-2, -1), norm=None)\n",
    "# cupy.fft.ifft2(a, s=None, axes=(-2, -1), norm=None)\n",
    "# cupy.fft.fftn(a, s=None, axes=None, norm=None)\n",
    "# cupy.fft.ifftn(a, s=None, axes=None, norm=None)\n",
    "# cupy.fft.rfft(a, n=None, axis=-1, norm=None)\n",
    "# cupy.fft.irfft(a, n=None, axis=-1, norm=None)\n",
    "# cupy.fft.rfft2(a, s=None, axes=(-2, -1), norm=None)\n",
    "# cupy.fft.irfft2(a, s=None, axes=(-2, -1), norm=None)\n",
    "# cupy.fft.rfftn(a, s=None, axes=None, norm=None)\n",
    "# cupy.fft.irfftn(a, s=None, axes=None, norm=None)\n",
    "# cupy.fft.hfft(a, n=None, axis=-1, norm=None)\n",
    "# cupy.fft.ihfft(a, n=None, axis=-1, norm=None)\n",
    "# cupy.fft.fftfreq(n, d=1.0)\n",
    "# cupy.fft.rfftfreq(n, d=1.0)\n",
    "# cupy.fft.fftshift(x, axes=None)\n",
    "# cupy.fft.ifftshift(x, axes=None)\n",
    "# class cupy.fft.config.set_cufft_callbacks(unicode cb_load=u'', unicode cb_store=u'', ndarray cb_load_aux_arr=None, *, ndarray cb_store_aux_arr=None)\n",
    "# cupy.fft.config.set_cufft_gpus(gpus)\n",
    "# cupy.fft.config.get_plan_cache()\n",
    "# cupy.fft.config.show_plan_cache_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1b640b5-a4a9-42b6-9b70-833f79589f16",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.apply_along_axis(func1d, axis, arr, *args, **kwargs)\n",
    "# class cupy.vectorize(pyfunc, otypes=None, doc=None, excluded=None, cache=False, signature=None)\n",
    "# cupy.piecewise(x, condlist, funclist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc45117a-82b9-44c6-a4f4-3413d9b09110",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.c_ = <cupy._indexing.generate.CClass object>\n",
    "# cupy.r_ = <cupy._indexing.generate.RClass object>\n",
    "# cupy.nonzero(a)\n",
    "# cupy.where(condition, x=None, y=None)\n",
    "# cupy.indices(dimensions, dtype=<class 'int'>)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ad2f5d6-e87f-4310-96e1-9ac6fe0bee31",
   "metadata": {},
   "outputs": [],
   "source": [
    "grid = cupy.indices((2, 3))\n",
    "grid.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c03b75c0-1bb7-4eb7-aee3-e33c76a0fe0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "grid[0]        # row indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9538e6d0-c0ba-4233-9b02-222bc2706777",
   "metadata": {},
   "outputs": [],
   "source": [
    "grid[1]        # column indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e04bd279-4df2-4a9a-a0a5-e4c7756ae2aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.mask_indices(n, mask_func, k=0)\n",
    "# cupy.tril_indices(n, k=0, m=None)\n",
    "# cupy.tril_indices_from(arr, k=0)\n",
    "# cupy.triu_indices(n, k=0, m=None)\n",
    "# cupy.triu_indices_from(arr, k=0)\n",
    "# cupy.ix_(*args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "591c3af5-7aa0-4884-ba65-5c047b45ca18",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = cupy.arange(10).reshape(2, 5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80411391-cb91-4884-8464-9aadb488fa35",
   "metadata": {},
   "outputs": [],
   "source": [
    "ixgrid = cupy.ix_([0,1], [2,4])\n",
    "ixgrid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58421804-4216-44a0-b2cd-4c1975342eff",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.ravel_multi_index(multi_index, dims, mode='wrap', order='C')\n",
    "cupy.ravel_multi_index(cupy.asarray([[3,6,6],[4,5,1]]), (7,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7173b73b-c141-4a38-a4ab-898d64b5110e",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.ravel_multi_index(cupy.asarray([[3,6,6],[4,5,1]]), (7,6),\n",
    "                       order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa875e7f-e3c2-467d-aad0-9a5888c82b4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.ravel_multi_index(cupy.asarray([[3,6,6],[4,5,1]]), (4,6),\n",
    "                       mode='clip')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "428292bf-700c-417d-aefa-e6f8150d941f",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.ravel_multi_index(cupy.asarray([[3,6,6],[4,5,1]]), (4,4),\n",
    "                       mode=('clip', 'wrap'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "655e47a6-0f7e-4b78-919c-0b829409bae2",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.ravel_multi_index(cupy.asarray((3,1,4,1)), (6,7,8,9))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09a48536-22a6-45ef-9418-131398d27b82",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.unravel_index(indices, dims, order='C')\n",
    "cupy.unravel_index(cupy.array([22, 41, 37]), (7, 6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac2cc372-f6f0-46c1-b6d0-04e3d35638d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.unravel_index(cupy.array([31, 41, 13]), (7, 6), order='F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bde80f8-f553-4fbc-9481-a56e05838fc6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.diag_indices(n, ndim=2)\n",
    "di = cupy.diag_indices(4)\n",
    "di"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47152d76-9b33-44ff-b0b7-2b7128fc15fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = cupy.arange(16).reshape(4, 4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "863cbf72-976e-414e-ac8a-5372f2b0a34d",
   "metadata": {},
   "outputs": [],
   "source": [
    "a[di] = 100\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f0bc186-a3fc-4245-ab6d-3ce311f339a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "d3 = cupy.diag_indices(2, 3)\n",
    "d3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42d29824-1b63-464e-9c72-008cc854abeb",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = cupy.zeros((2, 2, 2), dtype=int)\n",
    "a[d3] = 1\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "974d228b-ceb3-467b-a8fe-aed8ccc7ec54",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.diag_indices_from(arr)\n",
    "# cupy.take(a, indices, axis=None, out=None)\n",
    "# cupy.take_along_axis(a, indices, axis)\n",
    "# cupy.choose(a, choices, out=None, mode='raise')\n",
    "# cupy.compress(condition, a, axis=None, out=None)\n",
    "# cupy.diagonal(a, offset=0, axis1=0, axis2=1)\n",
    "# cupy.select(condlist, choicelist, default=0)\n",
    "# cupy.lib.stride_tricks.as_strided(x, shape=None, strides=None)\n",
    "# cupy.place(arr, mask, vals)\n",
    "arr = np.arange(6).reshape(2, 3)\n",
    "np.place(arr, arr>2, [44, 55])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf3be176-b0a8-44d0-81bd-0769a64bc107",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.put(a, ind, v, mode='wrap')\n",
    "# cupy.putmask(a, mask, values)\n",
    "x = cupy.arange(6).reshape(2, 3)\n",
    "cupy.putmask(x, x>2, x**2)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fc17184-a812-42cf-bae7-c4b4506d192f",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = cupy.arange(8)\n",
    "cupy.putmask(x, x>2, cupy.array([-33, -44]))\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5db8c3a6-f97f-4a3b-a109-b336a1a099db",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.fill_diagonal(a, val, wrap=False)\n",
    "a = cupy.zeros((3, 3), int)\n",
    "cupy.fill_diagonal(a, 5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cdd4747-f66e-4232-acee-4b133146d1d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# class cupy.flatiter(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7e501c3-4d96-4b8e-a7e5-b17f98f6ebac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.load(file, mmap_mode=None, allow_pickle=None)\n",
    "# cupy.save(file, arr, allow_pickle=None)\n",
    "# cupy.savez(file, *args, **kwds)\n",
    "# cupy.savez_compressed(file, *args, **kwds)\n",
    "# cupy.savetxt(fname, X, *args, **kwargs)\n",
    "# cupy.genfromtxt(*args, **kwargs)\n",
    "# cupy.array2string(a, *args, **kwargs)\n",
    "# cupy.array_repr(arr, max_line_width=None, precision=None, suppress_small=None)\n",
    "# cupy.array_str(arr, max_line_width=None, precision=None, suppress_small=None)\n",
    "# cupy.format_float_positional(x, *args, **kwargs)\n",
    "# cupy.format_float_scientific(x, *args, **kwargs)\n",
    "# cupy.base_repr(number, base=2, padding=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4be84edb-2191-4129-80d4-cc5e7551c996",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.dot(a, b, out=None)\n",
    "# cupy.vdot(a, b)\n",
    "# cupy.inner(a, b)\n",
    "# cupy.outer(a, b, out=None)\n",
    "# cupy.tensordot(a, b, axes=2)\n",
    "# cupy.einsum(subscripts, *operands, dtype=None, optimize=False)\n",
    "# cupy.linalg.matrix_power(M, n)\n",
    "# cupy.kron(a, b)\n",
    "# cupy.linalg.cholesky(a)\n",
    "# cupy.linalg.qr(a, mode='reduced')\n",
    "# cupy.linalg.svd(a, full_matrices=True, compute_uv=True)\n",
    "# cupy.linalg.eigh(a, UPLO='L')\n",
    "# cupy.linalg.eigvalsh(a, UPLO='L')\n",
    "# cupy.linalg.norm(x, ord=None, axis=None, keepdims=False)\n",
    "# cupy.linalg.det(a)\n",
    "# cupy.linalg.matrix_rank(M, tol=None)\n",
    "# cupy.linalg.slogdet(a)\n",
    "# cupy.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)\n",
    "# cupy.linalg.solve(a, b)\n",
    "# cupy.linalg.tensorsolve(a, b, axes=None)\n",
    "# cupy.linalg.lstsq(a, b, rcond='warn')\n",
    "# cupy.linalg.inv(a)\n",
    "# cupy.linalg.pinv(a, rcond=1e-15)\n",
    "# cupy.linalg.tensorinv(a, ind=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbe7b810-8b83-4dc1-bb69-b385c71866ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.all(a, axis=None, out=None, keepdims=False)\n",
    "# cupy.any(a, axis=None, out=None, keepdims=False)\n",
    "# cupy.union1d(arr1, arr2)\n",
    "# cupy.isneginf(x, out=None)\n",
    "cupy.isneginf(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "822963db-1360-44cd-bcf2-b81c76cd6b4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.isneginf(-cupy.inf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86b7baed-ab1a-4261-b80c-d84b77640187",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.isneginf(cupy.array([-cupy.inf, -4, cupy.nan, 0, 4, cupy.inf]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98bbb148-8130-4c0c-80f6-7e9e059b0299",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.isposinf(x, out=None)\n",
    "cupy.isposinf(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "375456d0-098c-4ec6-9bac-4483242a291f",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.isposinf(cupy.inf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d0430fb-9c2b-4f87-937b-ab8bc354b66c",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.isposinf(cupy.array([-cupy.inf, -4, cupy.nan, 0, 4, cupy.inf]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7aeab94e-9b13-41ae-a35b-41b6249ca24b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.iscomplex(x)\n",
    "cupy.iscomplex(cupy.array([1+1j, 1+0j, 4.5, 3, 2, 2j]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22d7092a-16b1-4a44-bcf9-4282a2ef9bc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.iscomplexobj(x)\n",
    "cupy.iscomplexobj(cupy.array([3, 1+0j, True]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2abc77e0-2c9a-4b5d-8eb3-ef1e0e1ddcbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.iscomplexobj(cupy.array([3, 1, True]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb4419d1-a7c4-4eb5-9269-cb12c28bf64f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.isfortran(a)\n",
    "a = cupy.array([[1, 2, 3], [4, 5, 6]], order='C')\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f254d48d-d777-407d-9da8-1c9b292e7ae7",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.isfortran(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64d46821-71ec-4258-a8fe-8c1729020c54",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = cupy.array([[1, 2, 3], [4, 5, 6]], order='F')\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ff4dc6c-e06c-40d9-8cf4-45cc4d3cc3b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.isfortran(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0269dabe-1eb0-47ce-bb02-1ec17c38e149",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = cupy.array([[1, 2, 3], [4, 5, 6]], order='C')\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7dd3394-0bfe-4560-8514-b3f379e8cf06",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.isfortran(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c062315b-f7a4-49e5-ad72-89ff6f622444",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = a.T\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f2cdb80-5209-4008-a0a9-418e927b4c84",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.isfortran(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1d58ef3-181b-4a50-b9b7-f159be1a71db",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.isfortran(np.array([1, 2], order='F'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a168315-6429-452a-9727-c984bf9e5640",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.isreal(x)\n",
    "cupy.isreal(cp.array([1+1j, 1+0j, 4.5, 3, 2, 2j]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7fb2241-67e8-4532-bb96-ec209a8caac8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.isrealobj(x)\n",
    "cupy.isrealobj(cupy.array([3, 1+0j, True]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac822313-d9c2-4b49-a88f-d92990123465",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.isrealobj(cupy.array([3, 1, True]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ce44b94-21c9-4bcb-b8ed-4a62dc00623c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.isscalar(element)\n",
    "# cupy.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)\n",
    "# cupy.isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)\n",
    "# cupy.array_equal(a1, a2, equal_nan=False)\n",
    "# cupy.array_equiv(a1, a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "751fcfeb-cbb2-4135-ad82-1623b367381b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.unwrap(p, discont=None, axis=-1, *, period=6.283185307179586)\n",
    "# cupy.around(a, decimals=0, out=None)\n",
    "# cupy.round_(a, decimals=0, out=None)\n",
    "# cupy.fix(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.prod(a, axis=None, dtype=None, out=None, keepdims=False)\n",
    "# cupy.sum(a, axis=None, dtype=None, out=None, keepdims=False)\n",
    "# cupy.nanprod(a, axis=None, dtype=None, out=None, keepdims=False)\n",
    "# cupy.nansum(a, axis=None, dtype=None, out=None, keepdims=False)\n",
    "# cupy.cumprod(a, axis=None, dtype=None, out=None)\n",
    "# cupy.cumsum(a, axis=None, dtype=None, out=None)\n",
    "# cupy.nancumprod(a, axis=None, dtype=None, out=None)\n",
    "# cupy.nancumsum(a, axis=None, dtype=None, out=None)\n",
    "# cupy.diff(a, n=1, axis=-1, prepend=None, append=None)\n",
    "# cupy.gradient(f, *varargs, axis=None, edge_order=1)\n",
    "# cupy.ediff1d(arr, to_end=None, to_begin=None)\n",
    "# cupy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)\n",
    "# cupy.trapz(y, x=None, dx=1.0, axis=-1)\n",
    "# cupy.i0(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.sinc(x, /, out=None, \\*, casting='same_kind', dtype=None)\n",
    "# cupy.angle(z, deg=False)\n",
    "# cupy.real(val)\n",
    "# cupy.imag(val)\n",
    "# cupy.convolve(a, v, mode='full')\n",
    "# cupy.clip(a, a_min, a_max, out=None)\n",
    "# cupy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)\n",
    "# cupy.real_if_close(a, tol=100)\n",
    "# cupy.interp(x, xp, fp, left=None, right=None, period=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "933434aa-178a-4e38-a3c1-a1fdc7de3cc6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.byte_bounds(a)\n",
    "# cupy.shares_memory(a, b, max_work=None)\n",
    "# cupy.may_share_memory(a, b, max_work=None)\n",
    "# cupy.show_config(*, _full=False)\n",
    "# cupy.who(vardict=None)\n",
    "a = cupy.arange(10)\n",
    "b = cupy.ones(20)\n",
    "cupy.who()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "187332a9-b75f-4865-a4f5-ef95ce2e5006",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {'x': cupy.arange(2.0),\n",
    "'y': cupy.arange(3.0), 'txt': 'Some str',\n",
    "'idx':5}\n",
    "cupy.who(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69b4631e-2cf6-4d4e-85f0-6a9a3d93cf14",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.pad(array, pad_width, mode='constant', **kwargs)\n",
    "a = cupy.array([1, 2, 3, 4, 5])\n",
    "cupy.pad(a, (2, 3), 'constant', constant_values=(4, 6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20cfa861-8b7e-472b-acc9-ac1d655c5edb",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.pad(a, (2, 3), 'edge')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "932a5425-5eff-4776-a111-d45815ee8027",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.pad(a, (2, 3), 'linear_ramp', end_values=(5, -4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c852b9cd-5684-4d72-ba05-9f3055844934",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.pad(a, (2,), 'maximum')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9594de6e-e6cf-4201-9efa-45fc994a48a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.pad(a, (2,), 'mean')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11c13531-f7ee-4f62-806c-d3f8b7f8504a",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = cupy.array([[1, 2], [3, 4]])\n",
    "cupy.pad(a, ((3, 2), (2, 3)), 'minimum')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f22ff167-0566-4468-9d13-c42b1d4f1e6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = cupy.array([1, 2, 3, 4, 5])\n",
    "cupy.pad(a, (2, 3), 'reflect')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a4484c9-f6ad-4b06-821a-bcfabc815f10",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.pad(a, (2, 3), 'reflect', reflect_type='odd')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "494197fb-64c7-4002-9d2d-26af5b448574",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.pad(a, (2, 3), 'symmetric')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0294ee2b-aff5-4ab9-a470-b2a986dfecb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.pad(a, (2, 3), 'symmetric', reflect_type='odd')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aaca797b-336e-4a82-9a47-9ef4c5501e65",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.pad(a, (2, 3), 'wrap')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7719cbe-565a-4fac-9e82-b143124e27e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def pad_with(vector, pad_width, iaxis, kwargs):\n",
    "    pad_value = kwargs.get('padder', 10)\n",
    "    vector[:pad_width[0]] = pad_value\n",
    "    vector[-pad_width[1]:] = pad_value\n",
    "a = cupy.arange(6)\n",
    "a = a.reshape((2, 3))\n",
    "cupy.pad(a, 2, pad_with)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38d9ce0a-87f2-4dbd-9afd-b145801babd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.pad(a, 2, pad_with, padder=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6986b20-3355-4cbd-a0f3-a0521a364b47",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.polynomial.polynomial.polyvander(x, deg)\n",
    "# cupy.polynomial.polynomial.polycompanion(c)\n",
    "# cupy.polynomial.polynomial.polyval(x, c, tensor=True)\n",
    "#   p(x) = c_0 + c_1 * x + ... + c_n * x^n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a883ee4e-1716-402f-af6c-165cb0341c98",
   "metadata": {},
   "source": [
    "$$ p(x) = c_0 + c_1 * x + ... + c_n * x^n $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebe7da2e-4491-4f6e-b0c7-4307ac9216b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.polynomial.polynomial.polyvalfromroots(x, r, tensor=True)\n",
    "#   p(x) = \\prod_{n=1}^{N} (x - r_n)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39c2f91b-c828-4035-8354-995b87d6c360",
   "metadata": {},
   "source": [
    "$$ p(x) = \\prod_{n=1}^{N} (x - r_n) $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35914a8f-79b4-4b00-994d-e38d85e11e83",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.polynomial.polyutils.as_series(alist, trim=True)\n",
    "# cupy.polynomial.polyutils.trimseq(seq)\n",
    "# cupy.polynomial.polyutils.trimcoef(c, tol=0)\n",
    "# class cupy.poly1d(c_or_r, r=False, variable=None)\n",
    "# cupy.poly(seq_of_zeros)\n",
    "# cupy.polyval(p, x)\n",
    "# cupy.roots(p)\n",
    "# cupy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)\n",
    "# cupy.polyadd(a1, a2)\n",
    "# cupy.polysub(a1, a2)\n",
    "# cupy.polymul(a1, a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85088775-baf9-49cc-ae7d-ec34d6aa1414",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.default_rng(seed=None)\n",
    "# class cupy.random.Generator(bit_generator)\n",
    "#   beta(self, a, b, size=None, dtype=numpy.float64)\n",
    "#   f(x) = \\frac{x^{\\alpha-1}(1-x)^{\\beta-1}}{B(\\alpha,\\beta)}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "140cc53c-dfa1-44d7-aee1-998d25104cf4",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{x^{\\alpha-1}(1-x)^{\\beta-1}}{B(\\alpha,\\beta)}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae073642-3b3d-4c39-b7d4-3b8be9485c80",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   binomial(self, n, p, size=None)\n",
    "#   f(x) = \\binom{n}{x}p^x(1-p)^(n-x)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8556fd6-8acb-4757-ad2f-55793a79b5dd",
   "metadata": {},
   "source": [
    "$$ f(x) = \\binom{n}{x}p^x(1-p)^(n-x). $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "671c3fe2-c16b-4597-90be-7748b8fd3e83",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   chisquare(self, df, size=None)\n",
    "#   f(x) = \\frac{(1/2)^{k/2}}{\\Gamma(k/2)}x^{k/2-1}e^{-x/2}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41e2ca0a-7972-42b8-b72a-a90d4388a83a",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{(1/2)^{k/2}}{\\Gamma(k/2)}x^{k/2-1}e^{-x/2}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba265a97-b9e1-48b3-9023-2256f415e8a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   dirichlet(self, alpha, size=None)\n",
    "#   f(x) = \\frac{\\Gamma(\\sum_{i=1}^K\\alpha_i)}                 {\\prod_{i=1}^{K}\\Gamma(\\alpha_i)}                 \\prod_{i=1}^Kx_i^{\\alpha_i-1}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d08460f6-117d-480e-9a3a-8d1e87b4943d",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{\\Gamma(\\sum_{i=1}^K\\alpha_i)} {\\prod_{i=1}^{K}\\Gamma(\\alpha_i)} \\prod_{i=1}^Kx_i^{\\alpha_i-1}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6238b7ee-9b05-459d-b687-1a09290cb419",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   exponential(self, scale=1.0, size=None)\n",
    "#   f(x) = \\frac{1}{\\beta}\\exp (-\\frac{x}{\\beta})."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4899690-3c6a-45b5-8a1d-0840549bb3b9",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{\\beta}\\exp (-\\frac{x}{\\beta}). $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f649bc66-5e10-4ed4-a257-17f7fc2c68f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   f(self, dfnum, dfden, size=None)\n",
    "#   f(x) = \\frac{1}{B(\\frac{d_1}{2},\\frac{d_2}{2})} \\left(\\frac{d_1}{d_2}\\right)^{\\frac{d_1}{2}} x^{\\frac{d_1}{2}-1} \\left(1+\\frac{d_1}{d_2}x\\right) ^{-\\frac{d_1+d_2}{2}}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc6a7db0-76e1-431b-ba09-b2d76776f3db",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{B(\\frac{d_1}{2},\\frac{d_2}{2})} \\left(\\frac{d_1}{d_2}\\right)^{\\frac{d_1}{2}} x^{\\frac{d_1}{2}-1} \\left(1+\\frac{d_1}{d_2}x\\right) ^{-\\frac{d_1+d_2}{2}}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91964da7-9735-425f-a5ff-4363bb76a8d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   gamma(self, shape, scale=1.0, size=None)\n",
    "#   f(x) = \\frac{1}{\\Gamma(k)\\theta^k}x^{k-1}e^{-x/\\theta}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaee1403-8753-4fb3-a502-5c04bf313c01",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{\\Gamma(k)\\theta^k}x^{k-1}e^{-x/\\theta}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a641d44-8500-4d7b-94be-a4c37018f18a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   geometric(self, p, size=None)\n",
    "#   f(x) = p(1-p)^{k-1}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be6dcb57-aab8-4da3-9e6c-657a6d992929",
   "metadata": {},
   "source": [
    "$$ f(x) = p(1-p)^{k-1}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dafce4d8-b528-4214-b68e-fc58628d2d49",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   hypergeometric(self, ngood, nbad, nsample, size=None)\n",
    "#   f(x) = \\frac{\\binom{m}{n}\\binom{N-m}{n-x}}{\\binom{N}{n}}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6091dd48-2d84-4b3f-9244-4ac86fd2cfb3",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{\\binom{m}{n}\\binom{N-m}{n-x}}{\\binom{N}{n}}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f82d2556-1b26-421a-b2d5-ccf48fbf1d9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   integers(self, low, high=None, size=None, dtype=numpy.int64, endpoint=False)\n",
    "#   logseries(self, p, size=None)\n",
    "#   f(x) = \\frac{-p^x}{x\\ln(1-p)}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bfcad07-55f0-4fde-94f1-150f44bb8d78",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{-p^x}{x\\ln(1-p)}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b2f05a0-dce7-4e16-819c-f905e74a7afe",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   poisson(self, lam=1.0, size=None)\n",
    "#   f(x) = \\frac{\\lambda^xe^{-\\lambda}}{x!}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49f405a9-8352-484f-97ee-c310f898a8e5",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{\\lambda^xe^{-\\lambda}}{x!}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08bfa187-5afc-4450-b9e2-13c03cc43e9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   power(self, a, size=None)\n",
    "#   f(x) = ax^{a-1}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59da64fd-3389-4d8e-a1cf-ac4db287d45f",
   "metadata": {},
   "source": [
    "$$ f(x) = ax^{a-1}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f6abaae-1654-4bdb-8001-f343cafe19df",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   random(self, size=None, dtype=numpy.float64, out=None)\n",
    "#   Unif[a, b), b > a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0eaa65ff-f711-433f-a085-6bb390be0134",
   "metadata": {},
   "source": [
    "$$ Unif[a, b), b > a $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f08c66d-7d80-4c29-b398-a4d509e346df",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   (b - a) * random() + a\n",
    "#   standard_exponential(self, size=None, dtype=numpy.float64, method='inv', out=None)\n",
    "#   f(x) = e^{-x}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50178071-8d0f-4122-85e0-6379b9905450",
   "metadata": {},
   "source": [
    "$$ f(x) = e^{-x}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd08f0aa-4c33-452a-bdc2-59e66c9846d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   standard_gamma(self, shape, size=None, dtype=numpy.float64, out=None)\n",
    "#   f(x) = \\frac{1}{\\Gamma(k)}x^{k-1}e^{-x}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5f3e491-3adb-4f79-9930-cb83b4e1409f",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{\\Gamma(k)}x^{k-1}e^{-x}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5a6e4a3-5989-4fcf-b706-7c3499e34fca",
   "metadata": {},
   "outputs": [],
   "source": [
    "#   standard_normal(self, size=None, dtype=numpy.float64, out=None)\n",
    "#   uniform(self, low=0.0, high=1.0, size=None, dtype=numpy.float64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae63e114-9a6a-453d-b0e1-341408bbbfed",
   "metadata": {},
   "outputs": [],
   "source": [
    "# class cupy.random.BitGenerator(seed=None)\n",
    "# class cupy.random.XORWOW(seed=None, *, size=-1)\n",
    "# class cupy.random.MRG32k3a(seed=None, *, size=-1)\n",
    "# class cupy.random.Philox4x3210(seed=None, *, size=-1)\n",
    "#   random_raw(self, size=None, output=True)\n",
    "# class cupy.random.RandomState(seed=None, method=None)\n",
    "#   beta(a, b, size=None, dtype=<class 'float'>)\n",
    "#   binomial(n, p, size=None, dtype=<class 'int'>)[source]\n",
    "#   cupy.random.binomial() for full documentation\n",
    "#   chisquare(df, size=None, dtype=<class 'float'>)[source]\n",
    "#   choice(a, size=None, replace=True, p=None)[source]\n",
    "#   dirichlet(alpha, size=None, dtype=<class 'float'>)[source]\n",
    "#   exponential(scale=1.0, size=None, dtype=<class 'float'>)[source]\n",
    "#   f(dfnum, dfden, size=None, dtype=<class 'float'>)[source]\n",
    "#   gamma(shape, scale=1.0, size=None, dtype=<class 'float'>)[source]\n",
    "#   geometric(p, size=None, dtype=<class 'int'>)[source]\n",
    "#   gumbel(loc=0.0, scale=1.0, size=None, dtype=<class 'float'>)[source]\n",
    "#   hypergeometric(ngood, nbad, nsample, size=None, dtype=<class 'int'>)[source]\n",
    "#   laplace(loc=0.0, scale=1.0, size=None, dtype=<class 'float'>)[source]\n",
    "#   logistic(loc=0.0, scale=1.0, size=None, dtype=<class 'float'>)[source]\n",
    "#   lognormal(mean=0.0, sigma=1.0, size=None, dtype=<class 'float'>)[source]\n",
    "#   logseries(p, size=None, dtype=<class 'int'>)[source]\n",
    "#   multivariate_normal(mean, cov, size=None, check_valid='ignore', tol=1e-08, method='cholesky', dtype=<class 'float'>)[source]\n",
    "#   negative_binomial(n, p, size=None, dtype=<class 'int'>)[source]\n",
    "#   noncentral_chisquare(df, nonc, size=None, dtype=<class 'float'>)[source]\n",
    "#   noncentral_f(dfnum, dfden, nonc, size=None, dtype=<class 'float'>)[source]\n",
    "#   normal(loc=0.0, scale=1.0, size=None, dtype=<class 'float'>)[source]\n",
    "#   pareto(a, size=None, dtype=<class 'float'>)[source]\n",
    "#   poisson(lam=1.0, size=None, dtype=<class 'int'>)[source]\n",
    "#   power(a, size=None, dtype=<class 'float'>)[source]\n",
    "#   rand(*size, **kwarg)[source]\n",
    "#   randint(low, high=None, size=None, dtype=<class 'int'>)[source]\n",
    "#   randn(*size, **kwarg)[source]\n",
    "#   random_sample(size=None, dtype=<class 'float'>)[source]\n",
    "#   rayleigh(scale=1.0, size=None, dtype=<class 'float'>)[source]\n",
    "#   seed(seed=None)[source]\n",
    "#   standard_cauchy(size=None, dtype=<class 'float'>)[source]\n",
    "#   standard_exponential(size=None, dtype=<class 'float'>)[source]\n",
    "#   standard_gamma(shape, size=None, dtype=<class 'float'>)[source]\n",
    "#   standard_normal(size=None, dtype=<class 'float'>)[source]\n",
    "#   standard_t(df, size=None, dtype=<class 'float'>)[source]\n",
    "#   tomaxint(size=None)[source]\n",
    "#   triangular(left, mode, right, size=None, dtype=<class 'float'>)[source]\n",
    "#   uniform(low=0.0, high=1.0, size=None, dtype=<class 'float'>)[source]\n",
    "#   vonmises(mu, kappa, size=None, dtype=<class 'float'>)[source]\n",
    "#   wald(mean, scale, size=None, dtype=<class 'float'>)[source]\n",
    "#   weibull(a, size=None, dtype=<class 'float'>)[source]\n",
    "#   zipf(a, size=None, dtype=<class 'int'>)[source]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ea2a67a-e6a3-46d9-8bb0-eafb1143d300",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.beta(a, b, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{x^{\\alpha-1}(1-x)^{\\beta-1}}{B(\\alpha,\\beta)}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de008cbe-b21d-4562-b3af-de6f72f6c70d",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{x^{\\alpha-1}(1-x)^{\\beta-1}}{B(\\alpha,\\beta)}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfacf93d-5683-4242-9a86-3cfaa50e5d3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.binomial(n, p, size=None, dtype=<class 'int'>)\n",
    "#   f(x) = \\binom{n}{x}p^x(1-p)^{n-x}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4357077-12cd-40ee-aba6-2311118ab00e",
   "metadata": {},
   "source": [
    "$$ f(x) = \\binom{n}{x}p^x(1-p)^{n-x}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bff2e18e-5c13-4f9f-a17c-6fc514d36789",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.bytes(length)\n",
    "# cupy.random.chisquare(df, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{(1/2)^{k/2}}{\\Gamma(k/2)}x^{k/2-1}e^{-x/2}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71e93b59-87b3-4bbc-aab0-09e1744e49a1",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{(1/2)^{k/2}}{\\Gamma(k/2)}x^{k/2-1}e^{-x/2}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffd3cae7-ddcb-4f8a-8e12-b14f518ea55f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.choice\n",
    "# cupy.random.dirichlet(alpha, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{\\Gamma(\\sum_{i=1}^K\\alpha_i)} {\\prod_{i=1}^{K}\\Gamma(\\alpha_i)} \\prod_{i=1}^Kx_i^{\\alpha_i-1}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b622210-52c1-4495-be48-0c0a9649c903",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{\\Gamma(\\sum_{i=1}^K\\alpha_i)} {\\prod_{i=1}^{K}\\Gamma(\\alpha_i)} \\prod_{i=1}^Kx_i^{\\alpha_i-1}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61d29be8-92ff-4187-a19a-4d1dbbb89aac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.exponential(scale, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{1}{\\beta}\\exp (-\\frac{x}{\\beta})."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b90524ea-68dd-4405-a56e-97ded7132661",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{\\beta}\\exp (-\\frac{x}{\\beta}). $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "185f9ed5-bb16-4a30-b8fe-a5c629ee29f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.f(dfnum, dfden, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{1}{B(\\frac{d_1}{2},\\frac{d_2}{2})} \\left(\\frac{d_1}{d_2}\\right)^{\\frac{d_1}{2}} x^{\\frac{d_1}{2}-1} \\left(1+\\frac{d_1}{d_2}x\\right) ^{-\\frac{d_1+d_2}{2}}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90d21ba6-3a97-405e-8e71-afb81fff1d1e",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{B(\\frac{d_1}{2},\\frac{d_2}{2})} \\left(\\frac{d_1}{d_2}\\right)^{\\frac{d_1}{2}} x^{\\frac{d_1}{2}-1} \\left(1+\\frac{d_1}{d_2}x\\right) ^{-\\frac{d_1+d_2}{2}}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cc0072c-cc12-4b00-92c6-fc88ebea35ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.gamma(shape, scale=1.0, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{1}{\\Gamma(k)\\theta^k}x^{k-1}e^{-x/\\theta}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "647c2c8f-4a1c-42cf-b492-0299f48ceb2f",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{\\Gamma(k)\\theta^k}x^{k-1}e^{-x/\\theta}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b800f1e-6d82-437c-a7dc-2f0783cd6012",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.geometric(p, size=None, dtype=<class 'int'>)\n",
    "#   f(x) = p(1-p)^{k-1}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3608db57-516c-4773-b6f4-2c6bc178f181",
   "metadata": {},
   "source": [
    "$$ f(x) = p(1-p)^{k-1}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5845dc6-c9d2-4877-97f3-ab21d4d19d60",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.gumbel(loc=0.0, scale=1.0, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{1}{\\eta} \\exp\\left\\{ - \\frac{x - \\mu}{\\eta} \\right\\} \\exp\\left[-\\exp\\left\\{-\\frac{x - \\mu}{\\eta} \\right\\}\\right],"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e7dae8e-dfc3-45a9-a91b-4f01dcbbad41",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{\\eta} \\exp\\left\\{ - \\frac{x - \\mu}{\\eta} \\right\\} \\exp\\left[-\\exp\\left\\{-\\frac{x - \\mu}{\\eta} \\right\\}\\right], $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc5325c9-403f-49be-9244-e6c4221f2d0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.hypergeometric(ngood, nbad, nsample, size=None, dtype=<class 'int'>)\n",
    "#   f(x) = \\frac{\\binom{m}{n}\\binom{N-m}{n-x}}{\\binom{N}{n}}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8adc0fa6-8674-4b56-9c47-c1d85ea0ea73",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{\\binom{m}{n}\\binom{N-m}{n-x}}{\\binom{N}{n}}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "566e44ae-064b-4870-8217-feff9fe24291",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.laplace(loc=0.0, scale=1.0, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{1}{2b}\\exp\\left(-\\frac{|x-\\mu|}{b}\\right)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "475c38eb-8163-4fba-8be9-c59543922cfc",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{2b}\\exp\\left(-\\frac{|x-\\mu|}{b}\\right). $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02028ed0-85e3-43af-a200-82a248d3795e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.logistic(loc=0.0, scale=1.0, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{e^{-(x-\\mu)/s}}{s(1+e^{-(x-\\mu)/s})^2}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88e29ae3-02c8-4b8a-9a2e-b9035a166182",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{e^{-(x-\\mu)/s}}{s(1+e^{-(x-\\mu)/s})^2}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac269a9c-43d6-4702-b2b5-34b45b90a326",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.lognormal(mean=0.0, sigma=1.0, size=None, dtype=<class 'float'>)\n",
    "# cupy.random.logseries(p, size=None, dtype=<class 'int'>)\n",
    "#   f(x) = \\frac{-p^x}{x\\ln(1-p)}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d62d1c5-127f-4f21-9515-34c970f66030",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{-p^x}{x\\ln(1-p)}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6d63820-86aa-41f1-ad1a-8df585ed820b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.multinomial(n, pvals, size=None)\n",
    "# cupy.random.multivariate_normal(mean, cov, size=None, check_valid='ignore', tol=1e-08, method='cholesky', dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{1}{(2\\pi|\\Sigma|)^(n/2)} \\exp\\left(-\\frac{1}{2} (x-\\mu)^{\\top}\\Sigma^{-1}(x-\\mu)\\right)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32772c7b-6af0-4d8d-917c-7db4a8eb243a",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{(2\\pi|\\Sigma|)^(n/2)} \\exp\\left(-\\frac{1}{2} (x-\\mu)^{\\top}\\Sigma^{-1}(x-\\mu)\\right). $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc6d86d3-f543-454f-8690-c3fa238894ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.negative_binomial(n, p, size=None, dtype=<class 'int'>)\n",
    "#   f(x) = \\binom{x + n - 1}{n - 1}p^n(1-p)^{x}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4770b5aa-5402-48e4-b4f5-f986cccbf52d",
   "metadata": {},
   "source": [
    "$$ f(x) = \\binom{x + n - 1}{n - 1}p^n(1-p)^{x}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3608384-63c2-4a8f-89fc-685b7d89f5f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.noncentral_chisquare(df, nonc, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{1}{2}e^{-(x+\\lambda)/2} \\left(\\frac{x}{\\lambda}\\right)^{k/4 - 1/2} I_{k/2 - 1}(\\sqrt{\\lambda x}),"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bc38eff-6da5-4720-9520-d6d52d89a6c4",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{2}e^{-(x+\\lambda)/2} \\left(\\frac{x}{\\lambda}\\right)^{k/4 - 1/2} I_{k/2 - 1}(\\sqrt{\\lambda x}), $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6163c469-8bac-4e64-8352-171bd1d85975",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.noncentral_f(dfnum, dfden, nonc, size=None, dtype=<class 'float'>)\n",
    "# cupy.random.normal(loc=0.0, scale=1.0, size=None, dtype=<class 'float'>)\n",
    "# cupy.random.pareto(a, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\alpha(1+x)^{-(\\alpha+1)}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d36dd348-4fd3-451d-94d7-d446dc4e321e",
   "metadata": {},
   "source": [
    "$$ f(x) = \\alpha(1+x)^{-(\\alpha+1)}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b3a489c-e2e5-44d2-95d5-ce3bcdb75cec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.permutation(a)\n",
    "# cupy.random.poisson(lam=1.0, size=None, dtype=<class 'int'>)\n",
    "#   f(x) = \\frac{\\lambda^xe^{-\\lambda}}{k!}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f61470db-bf02-4c09-aceb-5340d3511e4f",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{\\lambda^xe^{-\\lambda}}{k!}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28e86d0a-1aa0-4296-8aad-c1b3f106cb5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.power(a, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = ax^{a-1}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa30ab6b-a68f-46ad-9e52-06a2e4102db7",
   "metadata": {},
   "source": [
    "$$ f(x) = ax^{a-1}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecceeb54-5b8b-4248-b161-783ca7ae2a78",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.rand(*size, **kwarg)\n",
    "cupy.random.rand(3, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52e38458-a542-4903-a2fd-36670d0d621c",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.random.rand(3, 2, dtype=cupy.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "611ae2e8-2cc6-43c9-bfee-acef22733573",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.randint(low, high=None, size=None, dtype='l')\n",
    "# cupy.random.randn(*size, **kwarg)\n",
    "cupy.random.randn(3, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "662ad444-ae5f-4f15-846b-edee65488cd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "cupy.random.randn(3, 2, dtype=cupy.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35ad01a2-4652-4100-8957-463df1cafd79",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.random(size=None, dtype=<class 'float'>)\n",
    "# cupy.random.random_integers(low, high=None, size=None)\n",
    "# cupy.random.random_sample(size=None, dtype=<class 'float'>)\n",
    "# cupy.random.ranf(size=None, dtype=<class 'float'>)\n",
    "# cupy.random.rayleigh(scale=1.0, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{x}{\\sigma^2}e^{\\frac{-x^2}{2-\\sigma^2}}, x \\ge 0."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb09223d-7805-49f4-8c94-f9d7fe5251a2",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{x}{\\sigma^2}e^{\\frac{-x^2}{2-\\sigma^2}}, x \\ge 0. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7760b330-808b-467b-9d7c-c3b50157055c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.sample(size=None, dtype=<class 'float'>)\n",
    "# cupy.random.seed(seed=None)\n",
    "# cupy.random.shuffle(a)\n",
    "# cupy.random.standard_cauchy(size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{1}{\\pi(1+x^2)}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8f1f720-3076-4bb0-8ede-fc9e42bfb28b",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{\\pi(1+x^2)}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "683635c5-80db-4c6e-855f-47983dbd549b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.standard_exponential(size=None, dtype=<class 'float'>)\n",
    "#   f(x) = e^{-x}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d03d3969-3d5a-4970-b60d-ccbb932f8266",
   "metadata": {},
   "source": [
    "$$ f(x) = e^{-x}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b01c6f6d-b7c1-4f83-8742-dbcbc88affa0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.standard_gamma(shape, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{1}{\\Gamma(k)}x^{k-1}e^{-x}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ffa3677-2494-4dd6-8263-8e6c3f49bdf3",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{1}{\\Gamma(k)}x^{k-1}e^{-x}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53d2e302-ecb9-490d-924a-d6c5f4c09db2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.standard_normal(size=None, dtype=<class 'float'>)\n",
    "# cupy.random.standard_t(df, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{\\Gamma(\\frac{\\nu+1}{2})} {\\sqrt{\\nu\\pi}\\Gamma(\\frac{\\nu}{2})} \\left(1 + \\frac{x^2}{\\nu} \\right)^{-(\\frac{\\nu+1}{2})}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf81b6a2-74e4-411e-94d0-6fe3b26d25bd",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{\\Gamma(\\frac{\\nu+1}{2})} {\\sqrt{\\nu\\pi}\\Gamma(\\frac{\\nu}{2})} \\left(1 + \\frac{x^2}{\\nu} \\right)^{-(\\frac{\\nu+1}{2})}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76fb22d9-4bcb-4e03-aaff-b1b40bcf6c7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.triangular(left, mode, right, size=None, dtype=<class 'float'>)\n",
    "#   \\begin{split}f(x) = \\begin{cases}\n",
    "#        \\frac{2(x-l)}{(r-l)(m-l)} & \\text{for } l \\leq x \\leq m, \\\\\n",
    "#        \\frac{2(r-x)}{(r-l)(r-m)} & \\text{for } m \\leq x \\leq r, \\\\\n",
    "#        0 & \\text{otherwise}.\n",
    "#      \\end{cases}\\end{split}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fd736ce-ef95-4abf-bb25-8f46a5fa7e4b",
   "metadata": {},
   "source": [
    "$$\n",
    "\\begin{split}f(x) = \\begin{cases}\n",
    "     \\frac{2(x-l)}{(r-l)(m-l)} & \\text{for } l \\leq x \\leq m, \\\\\n",
    "     \\frac{2(r-x)}{(r-l)(r-m)} & \\text{for } m \\leq x \\leq r, \\\\\n",
    "     0 & \\text{otherwise}.\n",
    "   \\end{cases}\\end{split}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37606dce-4b97-46f1-a702-d997a3589724",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.uniform(low=0.0, high=1.0, size=None, dtype=<class 'float'>)\n",
    "# cupy.random.vonmises(mu, kappa, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\frac{e^{\\kappa \\cos(x-\\mu)}}{2\\pi I_0(\\kappa)}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a5a37b4-99f9-407b-9e0c-87bdadfaad76",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{e^{\\kappa \\cos(x-\\mu)}}{2\\pi I_0(\\kappa)}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "493c9efb-b1c9-4880-81c4-838e9b731ec3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.wald(mean, scale, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = \\sqrt{\\frac{\\lambda}{2\\pi x^3}} e^{\\frac{-\\lambda(x-\\mu)^2}{2\\mu^2x}}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dff93cc0-3844-470c-8a94-e73cd2fb7dca",
   "metadata": {},
   "source": [
    "$$ f(x) = \\sqrt{\\frac{\\lambda}{2\\pi x^3}} e^{\\frac{-\\lambda(x-\\mu)^2}{2\\mu^2x}}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed2bac7a-8704-4f8d-b9e8-44f1b28f2986",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.weibull(a, size=None, dtype=<class 'float'>)\n",
    "#   f(x) = ax^{(a-1)}e^{-x^a}."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95f5b86a-07ec-4a7c-b4eb-e0969a3e5792",
   "metadata": {},
   "source": [
    "$$ f(x) = ax^{(a-1)}e^{-x^a}. $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "965b1e44-9bb7-4c26-94f0-85240b477667",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.zipf(a, size=None, dtype=<class 'int'>)\n",
    "#   f(x) = \\frac{x^{-a}}{ \\zeta (a)},"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3ac6873-35fb-4686-8cab-26d60c508edf",
   "metadata": {},
   "source": [
    "$$ f(x) = \\frac{x^{-a}}{ \\zeta (a)}, $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a012bb04-5635-4ed6-86ac-7cc6b0c1cfa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.random.get_random_state()\n",
    "# cupy.random.set_random_state(rs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d7b192b-62af-4bf7-9dd2-2150d214f23b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.in1d(ar1, ar2, assume_unique=False, invert=False)\n",
    "# cupy.intersect1d(arr1, arr2, assume_unique=False, return_indices=False)\n",
    "# cupy.isin(element, test_elements, assume_unique=False, invert=False)\n",
    "# cupy.setdiff1d(ar1, ar2, assume_unique=False)\n",
    "# cupy.setxor1d(ar1, ar2, assume_unique=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecbcd407-26d1-42a0-90db-957eedec2186",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.sort(a, axis=-1, kind=None)\n",
    "# cupy.lexsort(keys)\n",
    "# cupy.argsort(a, axis=-1, kind=None)\n",
    "# cupy.msort(a)\n",
    "# cupy.sort_complex(a)\n",
    "# cupy.partition(a, kth, axis=-1)\n",
    "# cupy.argpartition(a, kth, axis=-1)\n",
    "# cupy.argmax(a, axis=None, dtype=None, out=None, keepdims=False)\n",
    "# cupy.nanargmax(a, axis=None, dtype=None, out=None, keepdims=False)\n",
    "# cupy.argmin(a, axis=None, dtype=None, out=None, keepdims=False)\n",
    "# cupy.nanargmin(a, axis=None, dtype=None, out=None, keepdims=False)\n",
    "# cupy.argwhere(a)\n",
    "# cupy.flatnonzero(a)\n",
    "# cupy.searchsorted(a, v, side='left', sorter=None)\n",
    "# cupy.extract(condition, a)\n",
    "# cupy.count_nonzero(a, axis=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a73339a-ac79-4a82-be55-91e9253d259b",
   "metadata": {},
   "source": [
    "# Statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "504206c0-74ee-4be4-9f9f-0687ed7271ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.amin(a, axis=None, out=None, keepdims=False)\n",
    "# cupy.amax(a, axis=None, out=None, keepdims=False)\n",
    "# cupy.nanmin(a, axis=None, out=None, keepdims=False)\n",
    "# cupy.nanmax(a, axis=None, out=None, keepdims=False)\n",
    "# cupy.ptp(a, axis=None, out=None, keepdims=False)\n",
    "# cupy.percentile(a, q, axis=None, out=None, overwrite_input=False, method='linear', keepdims=False, *, interpolation=None)\n",
    "# cupy.quantile(a, q, axis=None, out=None, overwrite_input=False, method='linear', keepdims=False, *, interpolation=None)\n",
    "# cupy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False)\n",
    "# cupy.average(a, axis=None, weights=None, returned=False, *, keepdims=False)\n",
    "# cupy.mean(a, axis=None, dtype=None, out=None, keepdims=False)\n",
    "# cupy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False)\n",
    "# cupy.var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False)\n",
    "# cupy.nanmedian(a, axis=None, out=None, overwrite_input=False, keepdims=False)\n",
    "# cupy.nanmean(a, axis=None, dtype=None, out=None, keepdims=False)\n",
    "# cupy.nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False)\n",
    "# cupy.nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False)\n",
    "# cupy.corrcoef(a, y=None, rowvar=True, bias=None, ddof=None, *, dtype=None)\n",
    "# cupy.correlate(a, v, mode='valid')\n",
    "# cupy.cov(a, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None, *, dtype=None)\n",
    "# cupy.histogram(x, bins=10, range=None, weights=None, density=False)\n",
    "# cupy.histogram2d(x, y, bins=10, range=None, weights=None, density=None)\n",
    "# cupy.histogramdd(sample, bins=10, range=None, weights=None, density=False)\n",
    "# cupy.bincount(x, weights=None, minlength=None)\n",
    "# cupy.digitize(x, bins, right=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "377aaf87-3b0f-4ee8-8d8d-2e6b489d7695",
   "metadata": {},
   "source": [
    "# Test support (cupy.testing)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5ff9adb-8d8a-4619-92b2-1340ed9b2415",
   "metadata": {},
   "source": [
    "## Asserts\n",
    "\n",
    "`assert_array_almost_equal(x, y[, decimal, ...])`\n",
    "\n",
    "Raises an AssertionError if objects are not equal up to desired precision.\n",
    "\n",
    "`assert_allclose(actual, desired[, rtol, ...])`\n",
    "\n",
    "Raises an AssertionError if objects are not equal up to desired tolerance.\n",
    "\n",
    "`assert_array_almost_equal_nulp(x, y[, nulp])`\n",
    "\n",
    "Compare two arrays relatively to their spacing.\n",
    "\n",
    "`assert_array_max_ulp(a, b[, maxulp, dtype])`\n",
    "\n",
    "Check that all items of arrays differ in at most N Units in the Last Place.\n",
    "\n",
    "`assert_array_equal(x, y[, err_msg, verbose, ...])`\n",
    "\n",
    "Raises an AssertionError if two array_like objects are not equal.\n",
    "\n",
    "`assert_array_less(x, y[, err_msg, verbose])`\n",
    "\n",
    "Raises an AssertionError if array_like objects are not ordered by less than.\n",
    "\n",
    "## CuPy-specific APIs\n",
    "\n",
    "### Asserts\n",
    "\n",
    "`assert_array_list_equal(xlist, ylist[, ...])`\n",
    "\n",
    "Compares lists of arrays pairwise with assert_array_equal.\n",
    "\n",
    "## NumPy-CuPy Consistency Check\n",
    "\n",
    "The following decorators are for testing consistency between CuPy’s functions and corresponding NumPy’s ones.\n",
    "\n",
    "`numpy_cupy_allclose([rtol, atol, err_msg, ...])`\n",
    "\n",
    "Decorator that checks NumPy results and CuPy ones are close.\n",
    "\n",
    "`numpy_cupy_array_almost_equal([decimal, ...])`\n",
    "\n",
    "Decorator that checks NumPy results and CuPy ones are almost equal.\n",
    "\n",
    "`numpy_cupy_array_almost_equal_nulp([nulp, ...])`\n",
    "\n",
    "Decorator that checks results of NumPy and CuPy are equal w.r.t.\n",
    "\n",
    "`numpy_cupy_array_max_ulp([maxulp, dtype, ...])`\n",
    "\n",
    "Decorator that checks results of NumPy and CuPy ones are equal w.r.t.\n",
    "\n",
    "`numpy_cupy_array_equal([err_msg, verbose, ...])`\n",
    "\n",
    "Decorator that checks NumPy results and CuPy ones are equal.\n",
    "\n",
    "`numpy_cupy_array_list_equal([err_msg, ...])`\n",
    "\n",
    "Decorator that checks the resulting lists of NumPy and CuPy's one are equal.\n",
    "\n",
    "`numpy_cupy_array_less([err_msg, verbose, ...])`\n",
    "\n",
    "Decorator that checks the CuPy result is less than NumPy result.\n",
    "\n",
    "## Parameterized dtype Test\n",
    "\n",
    "The following decorators offer the standard way for parameterized test with respect to single or the combination of dtype(s).\n",
    "\n",
    "`for_dtypes(dtypes[, name])`\n",
    "\n",
    "Decorator for parameterized dtype test.\n",
    "\n",
    "`for_all_dtypes([name, no_float16, no_bool, ...])`\n",
    "\n",
    "Decorator that checks the fixture with all dtypes.\n",
    "\n",
    "`for_float_dtypes([name, no_float16])`\n",
    "\n",
    "Decorator that checks the fixture with float dtypes.\n",
    "\n",
    "`for_signed_dtypes([name])`\n",
    "\n",
    "Decorator that checks the fixture with signed dtypes.\n",
    "\n",
    "`for_unsigned_dtypes([name])`\n",
    "\n",
    "Decorator that checks the fixture with unsigned dtypes.\n",
    "\n",
    "`for_int_dtypes([name, no_bool])`\n",
    "\n",
    "Decorator that checks the fixture with integer and optionally bool dtypes.\n",
    "\n",
    "`for_complex_dtypes([name])`\n",
    "\n",
    "Decorator that checks the fixture with complex dtypes.\n",
    "\n",
    "`for_dtypes_combination(types[, names, full])`\n",
    "\n",
    "Decorator that checks the fixture with a product set of dtypes.\n",
    "\n",
    "`for_all_dtypes_combination([names, ...])`\n",
    "\n",
    "Decorator that checks the fixture with a product set of all dtypes.\n",
    "\n",
    "`for_signed_dtypes_combination([names, full])`\n",
    "\n",
    "Decorator for parameterized test w.r.t.\n",
    "\n",
    "`for_unsigned_dtypes_combination([names, full])`\n",
    "\n",
    "Decorator for parameterized test w.r.t.\n",
    "\n",
    "`for_int_dtypes_combination([names, no_bool, ...])`\n",
    "\n",
    "Decorator for parameterized test w.r.t.\n",
    "\n",
    "## Parameterized order Test\n",
    "\n",
    "The following decorators offer the standard way to parameterize tests with orders.\n",
    "\n",
    "`for_orders(orders[, name])`\n",
    "\n",
    "Decorator to parameterize tests with order.\n",
    "\n",
    "`for_CF_orders([name])`\n",
    "\n",
    "Decorator that checks the fixture with orders 'C' and 'F'."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46fa6d9d-8100-4391-8d52-46045dad5399",
   "metadata": {},
   "source": [
    "# Window functions\n",
    "\n",
    "NumPy API Reference: Window functions\n",
    "\n",
    "## Various windows\n",
    "\n",
    "`bartlett(M)`\n",
    "\n",
    "Returns the Bartlett window.\n",
    "\n",
    "`blackman(M)`\n",
    "\n",
    "Returns the Blackman window.\n",
    "\n",
    "`hamming(M)`\n",
    "\n",
    "Returns the Hamming window.\n",
    "\n",
    "`hanning(M)`\n",
    "\n",
    "Returns the Hanning window.\n",
    "\n",
    "`kaiser(M, beta)`\n",
    "\n",
    "Return the Kaiser window."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2318da95-4bb2-4bdf-8157-35832861ed3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.bartlett(M)\n",
    "#   w(n) = \\frac{2}{M-1} \\left(\n",
    "#   \\frac{M-1}{2} - \\left|n - \\frac{M-1}{2}\\right|\n",
    "#   \\right)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c51557bd-7b23-4ac2-a0ea-6ef9b1330342",
   "metadata": {},
   "source": [
    "$$\n",
    "w(n) = \\frac{2}{M-1} \\left(\n",
    "\\frac{M-1}{2} - \\left|n - \\frac{M-1}{2}\\right|\n",
    "\\right)\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9709eb4-0893-492a-a4d5-207b52364802",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.blackman(M)\n",
    "#   w(n) = 0.42 - 0.5 \\cos\\left(\\frac{2\\pi{n}}{M-1}\\right)\n",
    "#   + 0.08 \\cos\\left(\\frac{4\\pi{n}}{M-1}\\right)\n",
    "#   \\qquad 0 \\leq n \\leq M-1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b14adc26-580d-43c3-a3f0-fd5d9c3bb429",
   "metadata": {},
   "source": [
    "$$\n",
    "w(n) = 0.42 - 0.5 \\cos\\left(\\frac{2\\pi{n}}{M-1}\\right)\n",
    "+ 0.08 \\cos\\left(\\frac{4\\pi{n}}{M-1}\\right)\n",
    "\\qquad 0 \\leq n \\leq M-1\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9e10ab8-123a-4d67-bbff-4ef6059782a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.hamming(M)\n",
    "#   w(n) = 0.54 - 0.46\\cos\\left(\\frac{2\\pi{n}}{M-1}\\right)\n",
    "#   \\qquad 0 \\leq n \\leq M-1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7817930-efbd-4166-b07a-44f698a3f4d3",
   "metadata": {},
   "source": [
    "$$\n",
    "w(n) = 0.54 - 0.46\\cos\\left(\\frac{2\\pi{n}}{M-1}\\right)\n",
    "\\qquad 0 \\leq n \\leq M-1\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8a98113-1918-4281-aaee-d7f7ae2e8b88",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.hanning(M)\n",
    "#   w(n) = 0.5 - 0.5\\cos\\left(\\frac{2\\pi{n}}{M-1}\\right)\n",
    "#   \\qquad 0 \\leq n \\leq M-1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a45beac5-ac87-4821-8788-1cabc5809ae3",
   "metadata": {},
   "source": [
    "$$\n",
    "w(n) = 0.5 - 0.5\\cos\\left(\\frac{2\\pi{n}}{M-1}\\right)\n",
    "\\qquad 0 \\leq n \\leq M-1\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "890fce9d-720d-417c-ba6a-2a70bb683a02",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cupy.kaiser(M, beta)\n",
    "#   w(n) = I_0\\left( \\beta \\sqrt{1-\\frac{4n^2}{(M-1)^2}}\n",
    "#   \\right)/I_0(\\beta)\n",
    "\n",
    "#   \\quad -\\frac{M-1}{2} \\leq n \\leq \\frac{M-1}{2}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f9fa36d-af2d-4b48-918e-2cd925c3edc8",
   "metadata": {},
   "source": [
    "$$\n",
    "w(n) = I_0\\left( \\beta \\sqrt{1-\\frac{4n^2}{(M-1)^2}}\n",
    "\\right)/I_0(\\beta)\n",
    "$$\n",
    "\n",
    "$$\n",
    "\\quad -\\frac{M-1}{2} \\leq n \\leq \\frac{M-1}{2}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "049773e2-1be4-4e29-8a8e-86b1cd9ac0ed",
   "metadata": {},
   "source": [
    "# Discrete Fourier transforms (cupyx.scipy.fft)\n",
    "\n",
    "SciPy API Reference: Discrete Fourier transforms (scipy.fft)\n",
    "\n",
    "## Fast Fourier Transforms (FFTs)\n",
    "\n",
    "`fft(x[, n, axis, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the one-dimensional FFT.\n",
    "\n",
    "`ifft(x[, n, axis, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the one-dimensional inverse FFT.\n",
    "\n",
    "`fft2(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the two-dimensional FFT.\n",
    "\n",
    "`ifft2(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the two-dimensional inverse FFT.\n",
    "\n",
    "`fftn(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the N-dimensional FFT.\n",
    "\n",
    "`ifftn(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the N-dimensional inverse FFT.\n",
    "\n",
    "`rfft(x[, n, axis, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the one-dimensional FFT for real input.\n",
    "\n",
    "`irfft(x[, n, axis, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the one-dimensional inverse FFT for real input.\n",
    "\n",
    "`rfft2(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the two-dimensional FFT for real input.\n",
    "\n",
    "`irfft2(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the two-dimensional inverse FFT for real input.\n",
    "\n",
    "`rfftn(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the N-dimensional FFT for real input.\n",
    "\n",
    "`irfftn(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the N-dimensional inverse FFT for real input.\n",
    "\n",
    "`hfft(x[, n, axis, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the FFT of a signal that has Hermitian symmetry.\n",
    "\n",
    "`ihfft(x[, n, axis, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the FFT of a signal that has Hermitian symmetry.\n",
    "\n",
    "`hfft2(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the FFT of a two-dimensional signal that has Hermitian symmetry.\n",
    "\n",
    "`ihfft2(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the Inverse FFT of a two-dimensional signal that has Hermitian symmetry.\n",
    "\n",
    "`hfftn(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the FFT of a N-dimensional signal that has Hermitian symmetry.\n",
    "\n",
    "`ihfftn(x[, s, axes, norm, overwrite_x, plan])`\n",
    "\n",
    "Compute the Inverse FFT of a N-dimensional signal that has Hermitian symmetry.\n",
    "\n",
    "## Discrete Cosine and Sine Transforms (DST and DCT)\n",
    "\n",
    "`dct(x[, type, n, axis, norm, overwrite_x])`\n",
    "\n",
    "Return the Discrete Cosine Transform of an array, x.\n",
    "\n",
    "`idct(x[, type, n, axis, norm, overwrite_x])`\n",
    "\n",
    "Return the Inverse Discrete Cosine Transform of an array, x.\n",
    "\n",
    "`dctn(x[, type, s, axes, norm, overwrite_x])`\n",
    "\n",
    "Compute a multidimensional Discrete Cosine Transform.\n",
    "\n",
    "`idctn(x[, type, s, axes, norm, overwrite_x])`\n",
    "\n",
    "Compute a multidimensional Discrete Cosine Transform.\n",
    "\n",
    "`dst(x[, type, n, axis, norm, overwrite_x])`\n",
    "\n",
    "Return the Discrete Sine Transform of an array, x.\n",
    "\n",
    "`idst(x[, type, n, axis, norm, overwrite_x])`\n",
    "\n",
    "Return the Inverse Discrete Sine Transform of an array, x.\n",
    "\n",
    "`dstn(x[, type, s, axes, norm, overwrite_x])`\n",
    "\n",
    "Compute a multidimensional Discrete Sine Transform.\n",
    "\n",
    "`idstn(x[, type, s, axes, norm, overwrite_x])`\n",
    "\n",
    "Compute a multidimensional Discrete Sine Transform.\n",
    "\n",
    "## Fast Hankel Transforms\n",
    "\n",
    "`fht(a, dln, mu[, offset, bias])`\n",
    "\n",
    "Compute the fast Hankel transform.\n",
    "\n",
    "`ifht(A, dln, mu[, offset, bias])`\n",
    "\n",
    "Compute the inverse fast Hankel transform.\n",
    "\n",
    "## Helper functions\n",
    "\n",
    "`fftshift(x[, axes])`\n",
    "\n",
    "Shift the zero-frequency component to the center of the spectrum.\n",
    "\n",
    "`ifftshift(x[, axes])`\n",
    "\n",
    "The inverse of fftshift().\n",
    "\n",
    "`fftfreq(n[, d])`\n",
    "\n",
    "Return the FFT sample frequencies.\n",
    "\n",
    "`rfftfreq(n[, d])`\n",
    "\n",
    "Return the FFT sample frequencies for real input.\n",
    "\n",
    "`next_fast_len(target[, real])`\n",
    "\n",
    "Find the next fast size to fft."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4301e89e-4800-42f6-b9bc-4bdb605d64d1",
   "metadata": {},
   "source": [
    "# Legacy discrete fourier transforms (cupyx.scipy.fftpack)\n",
    "\n",
    "## Fast Fourier Transforms (FFTs)\n",
    "\n",
    "`fft(x[, n, axis, overwrite_x, plan])`\n",
    "\n",
    "Compute the one-dimensional FFT.\n",
    "\n",
    "`ifft(x[, n, axis, overwrite_x, plan])`\n",
    "\n",
    "Compute the one-dimensional inverse FFT.\n",
    "\n",
    "`fft2(x[, shape, axes, overwrite_x, plan])`\n",
    "\n",
    "Compute the two-dimensional FFT.\n",
    "\n",
    "`ifft2(x[, shape, axes, overwrite_x, plan])`\n",
    "\n",
    "Compute the two-dimensional inverse FFT.\n",
    "\n",
    "`fftn(x[, shape, axes, overwrite_x, plan])`\n",
    "\n",
    "Compute the N-dimensional FFT.\n",
    "\n",
    "`ifftn(x[, shape, axes, overwrite_x, plan])`\n",
    "\n",
    "Compute the N-dimensional inverse FFT.\n",
    "\n",
    "`rfft(x[, n, axis, overwrite_x, plan])`\n",
    "\n",
    "Compute the one-dimensional FFT for real input.\n",
    "\n",
    "`irfft(x[, n, axis, overwrite_x])`\n",
    "\n",
    "Compute the one-dimensional inverse FFT for real input.\n",
    "\n",
    "`get_fft_plan(a[, shape, axes, value_type])`\n",
    "\n",
    "Generate a CUDA FFT plan for transforming up to three axes."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b13f6b41-bec5-4c57-ba10-221d76ef6716",
   "metadata": {},
   "source": [
    "# Interpolation (cupyx.scipy.interpolate)\n",
    "\n",
    "## Univariate interpolation\n",
    "\n",
    "`BarycentricInterpolator(xi[, yi, axis])`\n",
    "\n",
    "The interpolating polynomial for a set of points.\n",
    "\n",
    "`KroghInterpolator(xi, yi[, axis])`\n",
    "\n",
    "Interpolating polynomial for a set of points.\n",
    "\n",
    "`barycentric_interpolate(xi, yi, x[, axis])`\n",
    "\n",
    "Convenience function for polynomial interpolation.\n",
    "\n",
    "`krogh_interpolate(xi, yi, x[, der, axis])`\n",
    "\n",
    "Convenience function for polynomial interpolation\n",
    "\n",
    "`pchip_interpolate(xi, yi, x[, der, axis])`\n",
    "\n",
    "Convenience function for pchip interpolation.\n",
    "\n",
    "`CubicHermiteSpline(x, y, dydx[, axis, ...])`\n",
    "\n",
    "Piecewise-cubic interpolator matching values and first derivatives.\n",
    "\n",
    "`PchipInterpolator(x, y[, axis, extrapolate])`\n",
    "\n",
    "PCHIP 1-D monotonic cubic interpolation.\n",
    "\n",
    "`Akima1DInterpolator(x, y[, axis])`\n",
    "\n",
    "Akima interpolator\n",
    "\n",
    "`PPoly(c, x[, extrapolate, axis])`\n",
    "\n",
    "Piecewise polynomial in terms of coefficients and breakpoints The polynomial between x[i] and x[i + 1] is written in the local power basis.\n",
    "\n",
    "`BPoly(c, x[, extrapolate, axis])`\n",
    "\n",
    "Piecewise polynomial in terms of coefficients and breakpoints.\n",
    "\n",
    "## 1-D Splines\n",
    "\n",
    "`BSpline(t, c, k[, extrapolate, axis])`\n",
    "\n",
    "Univariate spline in the B-spline basis.\n",
    "\n",
    "`make_interp_spline(x, y[, k, t, bc_type, ...])`\n",
    "\n",
    "Compute the (coefficients of) interpolating B-spline.\n",
    "\n",
    "`splder(tck[, n])`\n",
    "\n",
    "Compute the spline representation of the derivative of a given spline\n",
    "\n",
    "`splantider(tck[, n])`\n",
    "\n",
    "Compute the spline for the antiderivative (integral) of a given spline.\n",
    "\n",
    "## Multivariate interpolation\n",
    "\n",
    "`RBFInterpolator(y, d[, neighbors, ...])`\n",
    "\n",
    "Radial basis function (RBF) interpolation in N dimensions.\n",
    "\n",
    "**For data on a grid:**\n",
    "\n",
    "`interpn(points, values, xi[, method, ...])`\n",
    "\n",
    "Multidimensional interpolation on regular or rectilinear grids.\n",
    "\n",
    "`RegularGridInterpolator(points, values[, ...])`\n",
    "\n",
    "Interpolation on a regular or rectilinear grid in arbitrary dimensions.\n",
    "\n",
    "**Tensor product polynomials:**\n",
    "\n",
    "`NdPPoly(c, x[, extrapolate])`\n",
    "\n",
    "Piecewise tensor product polynomial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fb1bf1d-22d5-4646-9ed0-a4cfa76489a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# class cupyx.scipy.interpolate.BSpline(t, c, k, extrapolate=True, axis=0)\n",
    "#   S(x) = \\sum_{j=0}^{n-1} c_j  B_{j, k; t}(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76941be0-b7fb-4ebc-87a7-80d1853180f3",
   "metadata": {},
   "source": [
    "$$ S(x) = \\sum_{j=0}^{n-1} c_j  B_{j, k; t}(x) $$\n",
    "\n",
    "$$\n",
    "\\begin{align}\\begin{aligned}B_{i, 0}(x) = 1, \\textrm{if $t_i \\le x < t_{i+1}$, otherwise $0$,}\\\\B_{i, k}(x) = \\frac{x - t_i}{t_{i+k} - t_i} B_{i, k-1}(x)\n",
    "         + \\frac{t_{i+k+1} - x}{t_{i+k+1} - t_{i+1}} B_{i+1, k-1}(x)\\end{aligned}\\end{align}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c762e6a9-6cd7-4d62-9822-32c941f7b282",
   "metadata": {},
   "outputs": [],
   "source": [
    "# class cupyx.scipy.interpolate.RBFInterpolator(y, d, neighbors=None, smoothing=0.0, kernel='thin_plate_spline', epsilon=None, degree=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72803cba-88eb-4b1d-b5c4-c7f6fb0f5a73",
   "metadata": {},
   "source": [
    "$$ f(x) = K(x, y) a + P(x) b, $$\n",
    "\n",
    "$$ (K(y, y) + \\lambda I) a + P(y) b = d $$\n",
    "\n",
    "$$ P(y)^T a = 0, $$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29b68cdb-3683-4bde-b57b-2ce40057a24c",
   "metadata": {},
   "source": [
    "# Linear algebra (cupyx.scipy.linalg)\n",
    "\n",
    "SciPy API Reference: Linear algebra (scipy.linalg)\n",
    "\n",
    "## Basics\n",
    "\n",
    "`solve_triangular(a, b[, trans, lower, ...])`\n",
    "\n",
    "Solve the equation a x = b for x, assuming a is a triangular matrix.\n",
    "\n",
    "`tril(m[, k])`\n",
    "\n",
    "Make a copy of a matrix with elements above the k-th diagonal zeroed.\n",
    "\n",
    "`triu(m[, k])`\n",
    "\n",
    "Make a copy of a matrix with elements below the k-th diagonal zeroed.\n",
    "\n",
    "## Matrix Functions\n",
    "\n",
    "`expm(a)`\n",
    "\n",
    "Compute the matrix exponential.\n",
    "\n",
    "## Decompositions\n",
    "\n",
    "`lu(a[, permute_l, overwrite_a, check_finite])`\n",
    "\n",
    "LU decomposition.\n",
    "\n",
    "`lu_factor(a[, overwrite_a, check_finite])`\n",
    "\n",
    "LU decomposition.\n",
    "\n",
    "`lu_solve(lu_and_piv, b[, trans, ...])`\n",
    "\n",
    "Solve an equation system, a * x = b, given the LU factorization of a\n",
    "\n",
    "## Special Matrices\n",
    "\n",
    "`block_diag(*arrs)`\n",
    "\n",
    "Create a block diagonal matrix from provided arrays.\n",
    "\n",
    "`circulant(c)`\n",
    "\n",
    "Construct a circulant matrix.\n",
    "\n",
    "`companion(a)`\n",
    "\n",
    "Create a companion matrix.\n",
    "\n",
    "`convolution_matrix(a, n[, mode])`\n",
    "\n",
    "Construct a convolution matrix.\n",
    "\n",
    "`dft(n[, scale])`\n",
    "\n",
    "Discrete Fourier transform matrix.\n",
    "\n",
    "`fiedler(a)`\n",
    "\n",
    "Returns a symmetric Fiedler matrix\n",
    "\n",
    "`fiedler_companion(a)`\n",
    "\n",
    "Returns a Fiedler companion matrix\n",
    "\n",
    "`hadamard(n[, dtype])`\n",
    "\n",
    "Construct an Hadamard matrix.\n",
    "\n",
    "`hankel(c[, r])`\n",
    "\n",
    "Construct a Hankel matrix.\n",
    "\n",
    "`helmert(n[, full])`\n",
    "\n",
    "Create an Helmert matrix of order n.\n",
    "\n",
    "`hilbert(n)`\n",
    "\n",
    "Create a Hilbert matrix of order n.\n",
    "\n",
    "`kron(a, b)`\n",
    "\n",
    "Kronecker product.\n",
    "\n",
    "`leslie(f, s)`\n",
    "\n",
    "Create a Leslie matrix.\n",
    "\n",
    "`toeplitz(c[, r])`\n",
    "\n",
    "Construct a Toeplitz matrix.\n",
    "\n",
    "`tri(N[, M, k, dtype])`\n",
    "\n",
    "Construct (N, M) matrix filled with ones at and below the k-th diagonal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc97117a-1505-4beb-805e-c220022f4a4d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
